我正在尝试使用 dbunit 来测试我的 DAO。我们在与 spring-test-dbunit 不兼容的版本中使用 Spring。我无法将我的 dao bean 自动装配到我的测试类中,因为那样我就必须使用@RunWith(SpringJUnit4ClassRunner.class)
关于一个无参数构造函数的 which。我的课程如下所示:
public class DbUnitExample extends DBTestCase {
@Autowired
public MyDAO myDAO;
public DbUnitExample(String name) {
super(name);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "...");
}
@Override
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSetBuilder().build(new FileInputStream("target/partial.xml"));
}
@Override
protected DatabaseOperation getSetUpOperation() throws Exception {
return DatabaseOperation.REFRESH;
}
@Override
protected DatabaseOperation getTearDownOperation() throws Exception {
return DatabaseOperation.NONE;
}
@Test
public void testSometing() throws Exception {
myDAO.deleteById(12662);
}
}
当然,我得到了一个 NPE,因为找不到我的 dao bean。当我使用时,@RunWith(SpringJUnit4ClassRunner.class)
我需要提供一个无参数构造函数并且必须删除我的“dbunit”构造函数。是否有标准方法或解决方法可以在不使用 spring-test-dbunit 的情况下将 dbunit 与 spring 一起使用
编辑
我的班级现在如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-application.xml")
@DirtiesContext
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
public class DbUnitExample extends DBTestCase {
@Autowired
public MyDAO myDAO;
public DbUnitExample() {
super("target/partial.xml");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "...");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "...");
}
@Override
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSetBuilder().build(new FileInputStream("target/partial.xml"));
}
@Override
protected DatabaseOperation getSetUpOperation() throws Exception {
return DatabaseOperation.REFRESH;
}
@Override
protected DatabaseOperation getTearDownOperation() throws Exception {
// return DatabaseOperation.NONE;
// return DatabaseOperation.REFRESH;
return DatabaseOperation.CLEAN_INSERT;
}
@Test
public void testSometing() throws Exception {
myDAO.deleteById(12662);
}
}
它现在可以编译,但没有 dbunt 功能,这意味着如果我删除一行,它不会恢复到之前的状态(再次插入)。