gpt4 book ai didi

java - DbUnit Java仅在一次测试中禁用FOREIGN_KEY_CHECKS

转载 作者:行者123 更新时间:2023-11-29 15:37:46 27 4
gpt4 key购买 nike

我在Java7中有一个带有Spring和DbUnit 2.5.4的Java项目。
我想禁用DbUnit键检查,但仅针对其中有许多表被交叉引用的特定测试。
我尝试了此代码,但没有成功。
如何仅为此测试禁用约束完整性?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:config/ioc/application-context-spring-test.xml"
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:config/ioc/application-context-spring-test.xml"
})
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
CleanInsertTestExecutionListener3A.class,
TransactionalTestExecutionListener.class, })
@TransactionConfiguration(
transactionManager = "myTxManager", defaultRollback = true)
public class FluxDSImplTest36466 {
.......

IDatabaseConnection dbConn = new DatabaseDataSourceConnection(
testContext.getApplicationContext().getBean(DataSource.class));
// BAD TEST
// dbConn.getConnection()
//.prepareStatement("set @@session.foreign_key_checks = 0").execute();
// dbConn.getConnection()
//.prepareStatement("set referential_integrity FALSE").execute();
// dbConn.getConnection()
//.prepareStatement("set foreign_key_checks=0;").execute();
// dbConn.getConnection()
//.prepareStatement("set foreign_key_checks=0").execute();
// dbConn.getConnection()
//.createStatement().execute("SET FOREIGN_KEY_CHECKS=0;");
//DatabaseOperation.CLEAN_INSERT.execute(dbConn, replaceDataSet);

最佳答案

如果您愿意尝试新的库,建议您使用dbrider,它具有这种功能并与Spring集成在一起,下面是一个示例:

@RunWith(SpringRunner.class)
@DBRider
@ContextConfiguration(classes = TestConfig.class)
public class DataSetIT {

@Autowired
private EntityUtils entityUtils;

@Test
@DataSet(value = "dataset.yml", cleanBefore = true, disableConstraints = true)
public void testCleanBefore() {
entityUtils.assertValues();
}
}


disableConstraints将在测试执行期间禁用所有约束,并向后启用它们。

关于java - DbUnit Java仅在一次测试中禁用FOREIGN_KEY_CHECKS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58055479/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com