DBUnit 不会在每个方法之后清理和插入数据库,因此测试不是独立的
我对 DAO 类进行了测试,我使用 DBUnit 创建并填充数据库(使用内存德比)。 我在测试 dao update 方法时遇到问题,因为它修改了数据,然后其他测试失败。我们都知道测试应该独立于其他测试,而且我知道 DBUnit 有一些工具可以在每次测试后清理和重新生成数据库。 但这不起作用!
代码是这样的(TestNG):
@BeforeMethod
public void prepareData() throws Exception {
cleanAndPopulate("users");
}
public void cleanAndPopulate (String nameXML) throws Exception {
IDatabaseConnection conn;
conn = new DatabaseConnection (sessionForTesting.connection());
InputStream is = DBconnection.class.getClassLoader()
.getResourceAsStream(nameXML + ".xml");
dataset = new FlatXmlDataSet(is);
System.out.println("*** Preparando base de datos de test");
DatabaseOperation.CLEAN_INSERT.execute(conn, dataset);
}
这是测试(禁用以避免附带影响):
@Test(enabled=false) // Deja la BBDD en estado erroneo!!!
public void busco_y_actualizo() throws Exception {
PacoUser resultado = userdao.getById(1L);
resultado.setName("OTRO");
userdao.update(resultado);
PacoUser resultado2 = userdao.getById(1L);
AssertJUnit.assertNotNull(resultado2);
AssertJUnit.assertEquals("OTRO", resultado2.getName());
}
I have a test for a DAO class, I use DBUnit to create and populate the database (using an in-memory derby).
I am in problems when testing the dao update method because it modify the data and then the other test fails. As all of us know a test should be independent of any other, and I know that DBUnit has some facilities to clean and regenerate the database after every test.
But it does not work!
The code is this (TestNG):
@BeforeMethod
public void prepareData() throws Exception {
cleanAndPopulate("users");
}
public void cleanAndPopulate (String nameXML) throws Exception {
IDatabaseConnection conn;
conn = new DatabaseConnection (sessionForTesting.connection());
InputStream is = DBconnection.class.getClassLoader()
.getResourceAsStream(nameXML + ".xml");
dataset = new FlatXmlDataSet(is);
System.out.println("*** Preparando base de datos de test");
DatabaseOperation.CLEAN_INSERT.execute(conn, dataset);
}
This is the test (disabled to avoid collateral effects):
@Test(enabled=false) // Deja la BBDD en estado erroneo!!!
public void busco_y_actualizo() throws Exception {
PacoUser resultado = userdao.getById(1L);
resultado.setName("OTRO");
userdao.update(resultado);
PacoUser resultado2 = userdao.getById(1L);
AssertJUnit.assertNotNull(resultado2);
AssertJUnit.assertEquals("OTRO", resultado2.getName());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 CLEAN_INSERT 在测试之前而不是测试之后执行“CLEAN”。
例如,如果有两个测试,test1 和 test2。 test1 和 test2 分别填充 test1.xml 和 test2.xml 中的表。
test1.xml 就像
test2.xml 就像
当测试的顺序是 test1 然后是 test2 时,CLEAN_INSERT 将执行以下操作:
因此,当执行 test2 时,table1 具有 test2.xml 中的数据,这正是我们所期望的。但table2仍然包含test1的数据,这可能会导致一些问题。
解决方法是为所有 xml 文件中的每个表设置一个空行。它将确保在插入之前清理所有表。
对于上面的例子,
test1.xml将像
test2.xml一样
It is because CLEAN_INSERT does "CLEAN" before the test, not after the test.
For example, if there are two tests, test1 and test2. test1 and test2 populate tables from test1.xml and test2.xml respectively.
test1.xml is like
test2.xml is like
When the order of tests is test1 and then test2, CLEAN_INSERT will do the following operations:
So when test2 is executed, table1 has data from test2.xml, which is what we expect. But table2 still contains data for test1, which may cause some issues.
A workaround is to have an empty row for each table in all xml files. It will make sure all tables would be cleaned before insert.
For the above example,
test1.xml will be like
test2.xml is like
确保在每个测试用例之前重置数据库以确保测试独立性。 @BeforeMethod 仅在所有测试用例运行之前调用一次,因此将
cleanAndPopulate
放在这里是不够的。Make sure that the database is reset before every single test case to ensure test independency. @BeforeMethod is only called once before all test cases are run, so putting
cleanAndPopulate
here is not enough.