如何进行非交易弹簧测试

时间:2013-10-05 07:22:53

标签: java spring testing transactions

我有这个测试类

@TransactionConfiguration(defaultRollback = false)
public class CandidateServiceTest extends AbstractServiceTest {
    @Test
    public void add() {
    }
    ...
}


@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" })
public class AbstractServiceTest  extends AbstractTransactionalJUnit4SpringContextTests{
 ...
}

所有方法CandidateServiceTest都是事务性的。如何更改CandidateServiceTest所有方法都是NonTransactional?

1 个答案:

答案 0 :(得分:1)

不要延长AbstractTransactionalJUnit4SpringContextTests。只需使用@Transactional注释,就像使用服务一样:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" })
public class AbstractServiceTest {

}

@TransactionConfiguration(defaultRollback = false)
public class CandidateServiceTest extends AbstractServiceTest {
    @Test
    public void add() {
        // non-transactional test
    }

    @Test
    @Transactional
    public void transactionalAdd() {
        // transactional test
    }
}