持续集成之路——数据访问层单元测试遇到的问题(2)

发表于:2013-07-22来源:Csdn作者:mydeman点击数: 标签:持续集成
at org.hsqldb.jdbc.Util.sqlException(Unknown Source) at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) Caused by: org.hsqldb.HsqlException: integrity constraint violation: foreign key no ac

  at org.hsqldb.jdbc.Util.sqlException(Unknown Source)

  at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)

  ……

  ……

  Caused by: org.hsqldb.HsqlException: integrity constraint violation: foreign key no action; FK_L6IDVK78B2TLU8NO6EDJ0G6U8 table: CUSTOM_TABLE_COLUMN_SPACE_TYPE

  at org.hsqldb.error.Error.error(Unknown Source)

  at org.hsqldb.StatementDML.performReferentialActions(Unknown Source)

  at org.hsqldb.StatementDML.delete(Unknown Source)

  at org.hsqldb.StatementDML.executeDeleteStatement(Unknown Source)

  at org.hsqldb.StatementDML.getResult(Unknown Source)

  at org.hsqldb.StatementDMQL.execute(Unknown Source)

  at org.hsqldb.Session.executeCompiledStatement(Unknown Source)

  at org.hsqldb.Session.executeDirectStatement(Unknown Source)

  at org.hsqldb.Session.execute(Unknown Source)

  at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)

  ……

  看异常信息,应该是删除记录时,外键级联导致的问题。在实体类里改变级联设置并不起作用。最后在StackOverflow上找了一个解决方法:编写一个类,继承AbstractTestExecutionListener,在beforeTestClass中取消级联依赖。具体如下:

  [java] view plaincopyprint?

  import org.dbunit.database.DatabaseDataSourceConnection;

  import org.dbunit.database.IDatabaseConnection;

  import org.springframework.test.context.TestContext;

  import org.springframework.test.context.support.AbstractTestExecutionListener;

  import javax.sql.DataSource;

  public class ForeignKeyDisabling extends AbstractTestExecutionListener {

  @Override

  public void beforeTestClass(TestContext testContext) throws Exception {

  IDatabaseConnection dbConn = new DatabaseDataSourceConnection(

  testContext.getApplicationContext().getBean(DataSource.class)

  );

  dbConn.getConnection().prepareStatement("SET DATABASE REFERENTIAL INTEGRITY FALSE").execute();

  }

  }

  import org.dbunit.database.DatabaseDataSourceConnection;

  import org.dbunit.database.IDatabaseConnection;

  import org.springframework.test.context.TestContext;

  import org.springframework.test.context.support.AbstractTestExecutionListener;

  import javax.sql.DataSource;

  public class ForeignKeyDisabling extends AbstractTestExecutionListener {

  @Override

  public void beforeTestClass(TestContext testContext) throws Exception {

  IDatabaseConnection dbConn = new DatabaseDataSourceConnection(

  testContext.getApplicationContext().getBean(DataSource.class)

  );

  dbConn.getConnection().prepareStatement("SET DATABASE REFERENTIAL INTEGRITY FALSE").execute();

  }

  }

  把这个新的Listener添加测试类的注解中:

  [java] view plaincopyprint?

  @RunWith(SpringJUnit4ClassRunner.class)

  @ContextConfiguration("classpath:applicationContext-test.xml")

  @TestExecutionListeners({

  DependencyInjectionTestExecutionListener.class,

  DirtiesContextTestExecutionListener.class,

  TransactionDbUnitTestExecutionListener.class,

  ForeignKeyDisabling.class})

  @RunWith(SpringJUnit4ClassRunner.class)

  @ContextConfiguration("classpath:applicationContext-test.xml")

  @TestExecutionListeners({

  DependencyInjectionTestExecutionListener.class,

  DirtiesContextTestExecutionListener.class,

  TransactionDbUnitTestExecutionListener.class,

  ForeignKeyDisabling.class})

  参考:http://stackoverflow.com/questions/2685274/tdd-with-hsqldb-removing-foreign-keys

  三、PROPERTY_DATATYPE_FACTORY引起的警告

  在jenkins中构建时,总是可以看到如下的警告信息:

  WARN getDataTypeFactory, Potential problem found: The configured data type factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' might cause problems with the current database 'HSQL Database Engine' (e.g. some datatypes may not be supported properly). In rare cases you might see this message because the list of supported database products is incomplete (list=[derby]). If so please request a java-class update via the forums.If you are using your own IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override getValidDbProducts() to specify the supported database products.

  意思很好理解,就说默认的DataTypeFactory可能会引起问题,建议设置该属性值。解决方法也很明显:就是设置数据库连接的PROPERTY_DATATYPE_FACTORY属性的值。尝试了用Before、BeforeClass或者自定义ExecutionListener中都无法实现对该属性的设置。

原文转自:http://blog.csdn.net/mydeman/article/details/9374621