1

I'm new to unitils(built in dbunit) and 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.

There is a push to use spring so I've been trying to merge the following functionality.

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 ).

Unitils tests using:

@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.

Problem is I can't seem to unitilise my class whilst using the spring version as they're both JUnit4ClassRunner extensions.

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

sapatos
  • 1,292
  • 3
  • 21
  • 44

2 Answers2

4

Unitils doesn't really have proper Spring integration support.

Have a look at spring-test-dbunit instead.

mmey
  • 1,545
  • 16
  • 24
  • +1, `Unitils` seems to be abandoned nowadays. Furthermore there is also alternative from [spring-dbunit](https://github.com/excilys/spring-dbunit). Both seems to provide the same functionality. – G. Demecki Oct 13 '14 at 13:40
3

Unitils has a Spring integration package:

org.unitils:unitils-spring:3.3

We've not had to migrate from Spring tests, but do use Spring and Unitils together. Rather than using the @RunWith annotation, we subclass:

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. I'm not really sure, but turning off transactions in Unitils by setting this property:

DatabaseModule.Transactional.value.default=disabled

in the unitils.properties file got around the problem.

Jeff French
  • 1,151
  • 1
  • 12
  • 19