[英]Problems using dbunit with Spring (without spring-test-dbunit)
I'm trying to use dbunit to test my DAOs. 我正在尝试使用dbunit测试我的DAO。 We use Spring in a version that is not compatible with spring-test-dbunit. 我们在与spring-test-dbunit不兼容的版本中使用Spring。 I can't autowire my dao beans into my test class, because then I would have to use @RunWith(SpringJUnit4ClassRunner.class)
which regards one parameterless constructor. 我不能将我的dao bean自动连接到测试类中,因为那样我就不得不使用@RunWith(SpringJUnit4ClassRunner.class)
,它涉及一个无参数的构造函数。 My class looks like following: 我的课如下:
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);
}
}
Of course I get an NPE because my dao bean can't be found. 当然,我得到了NPE,因为找不到我的刀豆。 When I use @RunWith(SpringJUnit4ClassRunner.class)
I need to provide one parameterless constructor and have to delete my "dbunit"-constructor. 当我使用@RunWith(SpringJUnit4ClassRunner.class)
我需要提供一个无参数的构造函数,并且必须删除我的“ dbunit”构造函数。 Is there a standard way or workaround to use dbunit with spring without the use of spring-test-dbunit 有没有在不使用spring-test-dbunit的情况下将dbunit与spring一起使用的标准方法或解决方法
EDIT 编辑
My class now looks like following: 我的课程现在如下所示:
@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);
}
}
It compiles now, but has no dbunt-functionality, which means if I delete a row it doesn't get restored to it's previous state (inserted again). 它现在可以编译,但是没有dbunt功能,这意味着如果我删除一行,它不会恢复到先前的状态(再次插入)。
Since you are using Spring, I suggest autowiring the dbUnit instances into the test. 由于您使用的是Spring,因此建议将dbUnit实例自动装配到测试中。 The dbUnit Test Cases page has "Configuration Example Using Spring" for the PrepAndExpectedTestCase
, but just copy the code and change it to DBTestCase
and adjust accordingly. 该DBUnit的测试用例页面有“配置示例使用Spring”为PrepAndExpectedTestCase
,而只是复制代码并将其更改为DBTestCase
并相应地调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.