替换导入org.springframework.test.context.transaction.TransactionConfiguration;在Spring测试4.3.1版本?

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

我正在处理 JPA QueryDSL 示例。在此示例中,我创建了 PersonDaoTest.java。早些时候我使用的是 spring-test 的较低版本,因为客户要求将其更新为最新版本,所以我使用了 4.3.1.RELEASE。当我使用这个版本时,我看到 import org.springframework.test.context.transaction.TransactionConfiguration; 是 @deprecated。谁能告诉我最新版本 import org.springframework.test.context.transaction.TransactionConfiguration; 的替代品是什么?

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"));
    }
}

文档存在是有原因的:作为开发人员,您应该阅读它!

Javadoc for @TransactionConfiguration 明确指出:

Deprecated.

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

因此,您问题的答案很简单@Rollback

但是...更重要的是,您声明的 @TransactionConfiguration(defaultRollback=true) 是无用的,因为无论如何默认值是 true。所以你可以完全删除它。

此致,

Sam(Spring TestContext Framework 的作者)