在单独的测试方法中对每个事务进行顺序集成测试

时间:2012-03-20 15:05:14

标签: spring transactions integration-testing

我能够成功获得一系列顺序事务以便在Spring集成测试中按时提交的唯一方法是将每个逻辑事务放在一个单独的@Transactional @Test方法中,并按顺序放置这些方法希望它们执行,所以我的测试类看起来像这样:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/test/resources/itest/integrationTestContext.xml"}) 
@TransactionConfiguration(transactionManager="myTxManager", defaultRollback=false)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) 

public class ITAccessPoint {

    @Test
    @Transactional
    public void transaction1() {
        myDAO.persist(obj1);
    }

    @Test
    public void testStep1() {
        /* code that depends on obj1 being persisted & committed */
    }

    @Test
    @Transactional
    public void transaction2() {
        myDAO.persist(obj2);
    }

    @Test
    public void testStep2() {
        /* code that depends on obj2 being persisted & committed */
    }
    .
    .
    .
}

这是一种可接受的编写涉及许多顺序数据库事务的集成测试的技术,还是这种做法很糟糕?

0 个答案:

没有答案