Search code examples
querydslspring-testspring-test-mvc

Replacement of import org.springframework.test.context.transaction.TransactionConfiguration; in Spring Test 4.3.1 version?


I'm working on the JPA QueryDSL example. In this example I created PersonDaoTest.java. Earlier I was using the lower version of spring-test as client asked to update it its latest version so I used 4.3.1.RELEASE. When I used this version I see that import org.springframework.test.context.transaction.TransactionConfiguration; is @deprecated. Could anyone please let me know whats the replacement of import org.springframework.test.context.transaction.TransactionConfiguration; in latest version ?

PersonDaoTest.java

@ContextConfiguration("/test-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class PersonDaoTest {

    @Autowired
    private PersonDao personDao;

    //

    @Test
    public void testCreation() {
        personDao.save(new Person("Erich", "Gamma"));
        final Person person = new Person("Kent", "Beck");
        personDao.save(person);
        personDao.save(new Person("Ralph", "Johnson"));

        final Person personFromDb = personDao.findPersonsByFirstnameQueryDSL("Kent").get(0);
        Assert.assertEquals(person.getId(), personFromDb.getId());
    }

    @Test
    public void testMultipleFilter() {
        personDao.save(new Person("Erich", "Gamma"));
        final Person person = personDao.save(new Person("Ralph", "Beck"));
        final Person person2 = personDao.save(new Person("Ralph", "Johnson"));

        final Person personFromDb = personDao.findPersonsByFirstnameAndSurnameQueryDSL("Ralph", "Johnson").get(0);
        Assert.assertNotSame(person.getId(), personFromDb.getId());
        Assert.assertEquals(person2.getId(), personFromDb.getId());
    }

    @Test
    public void testOrdering() {
        final Person person = personDao.save(new Person("Kent", "Gamma"));
        personDao.save(new Person("Ralph", "Johnson"));
        final Person person2 = personDao.save(new Person("Kent", "Zivago"));

        final Person personFromDb = personDao.findPersonsByFirstnameInDescendingOrderQueryDSL("Kent").get(0);
        Assert.assertNotSame(person.getId(), personFromDb.getId());
        Assert.assertEquals(person2.getId(), personFromDb.getId());
    }

    @Test
    public void testMaxAge() {
        personDao.save(new Person("Kent", "Gamma", 20));
        personDao.save(new Person("Ralph", "Johnson", 35));
        personDao.save(new Person("Kent", "Zivago", 30));

        final int maxAge = personDao.findMaxAge();
        Assert.assertTrue(maxAge == 35);
    }

    @Test
    public void testMaxAgeByName() {
        personDao.save(new Person("Kent", "Gamma", 20));
        personDao.save(new Person("Ralph", "Johnson", 35));
        personDao.save(new Person("Kent", "Zivago", 30));

        final Map<String, Integer> maxAge = personDao.findMaxAgeByName();
        Assert.assertTrue(maxAge.size() == 2);
        Assert.assertSame(35, maxAge.get("Ralph"));
        Assert.assertSame(30, maxAge.get("Kent"));
    }
}

enter image description here


Solution

  • Documentation exists for a reason: as a developer you are expected to read it!

    The Javadoc for @TransactionConfiguration explicitly states:

    Deprecated.

    As of Spring Framework 4.2, use @Rollback or @Commit at the class level and the transactionManager qualifier in @Transactional.

    Thus, the answer to your question is simply @Rollback.

    But... more importantly, your declaration of @TransactionConfiguration(defaultRollback=true) is useless since the default is true anyway. So you can just delete that altogether.

    Regards,

    Sam (author of the Spring TestContext Framework)