简体   繁体   English

使用unitils,dbunit和spring进行单元测试

[英]unit testing with unitils, dbunit and spring

I'm new to unitils(built in dbunit) and spring. 我是unitils(内置于dbunit中)和spring的新手。 At the new place I'm working I can see that some of the testing is with spring and others using unitils/dbunit without spring. 在新工作的地方,我可以看到一些测试是通过spring进行的,而另一些测试是在不使用spring的情况下使用unitils / dbunit进行的。

There is a push to use spring so I've been trying to merge the following functionality. 有一个使用spring的推动,所以我一直在尝试合并以下功能。

Spring tests using: 春季测试使用:

@RunWith(SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations = { "classpath:spring-dev.xml" })
public class x(){
@Autowired
private ProfileDao profileDao;

@Test
@Rollback
@Transactional
public void shouldRetrieveProfileByUserNumber() {
    Profile profile = profileDao.retrieveProfile("u1", "0000003861", null);
    assertThat(profile.getUserNumber(), is("0000003861"));
}

This class has the @Autowired ProfileDao, which works when using the @RunWith(SpringJUnit4ClassRunner.class ). 此类具有@Autowired ProfileDao,在使用@RunWith(SpringJUnit4ClassRunner.class)时可以使用。

Unitils tests using: Unitils测试使用:

@RunWith(UnitilsJUnit4TestClassRunner.class)
@DataSet("WEB_SERVICE_DATASET.xml")
public class ServiceExceptionTest {}

Which correctly loads the database etc when run. 运行时正确加载数据库等。

What I'm trying to achieve is to combine these so that I can "Unitil'ise" my class with the @RunWith(UnitilsJUnit4TestClassRunner.class) annotation but also make use of spring in the normal way as in the previous class. 我想要实现的是将这些结合起来,以便可以将我的类与@RunWith(UnitilsJUnit4TestClassRunner.class)注释“统一”,但也可以像上一类一样以正常方式使用spring。

Problem is I can't seem to unitilise my class whilst using the spring version as they're both JUnit4ClassRunner extensions. 问题是在使用春季版本时,我似乎无法统一我的班级,因为它们都是JUnit4ClassRunner扩展。

I've tried a variety of variations but have not been able to get any to work. 我尝试了各种变体,但没有任何作用。

Can anyone advise on a decent way of doing this? 有人可以建议这样做的体面方法吗?

Thanks 谢谢

Unitils doesn't really have proper Spring integration support. Unitils确实没有适当的Spring集成支持。

Have a look at spring-test-dbunit instead. 请改为查看spring-test-dbunit

Unitils has a Spring integration package: Unitils有一个Spring集成包:

org.unitils:unitils-spring:3.3

We've not had to migrate from Spring tests, but do use Spring and Unitils together. 我们不必从Spring测试中迁移,但可以将Spring和Unitils一起使用。 Rather than using the @RunWith annotation, we subclass: 我们不是使用@RunWith批注,而是将其子类化:

import org.unitils.UnitilsJUnit4;
import org.unitils.dbunit.annotation.DataSet;
import org.unitils.spring.annotation.SpringBean;
import org.unitils.spring.annotation.SpringApplicationContext;

@SpringApplicationContext({"main-context.xml", "test-context.xml"})
@DataSet
public class ServiceTest extends UnitilsJUnit4 {

  @SpringBean("ourServiceBean")
  OurService ourService;

  @Test
  public void someTest() throws Exception {
  }
}

We recently ran into a problem where our test data wasn't being loaded properly the first time. 最近,我们遇到了一个问题,即我们的测试数据第一次没有正确加载。 This could be a conflict between the Unitils and Spring transaction handling. 这可能是Unitils和Spring事务处理之间的冲突。 I'm not really sure, but turning off transactions in Unitils by setting this property: 我不确定,但是可以通过设置以下属性来关闭Unitils中的事务:

DatabaseModule.Transactional.value.default=disabled

in the unitils.properties file got around the problem. 在unitils.properties文件中解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM