简体   繁体   English

如果没有Spring事务测试支持,如何编写Junit4测试?

[英]How do i write Junit4 tests without Spring transactional test support?

I would like to test transaction rollbacks in my application service. 我想在我的应用程序服务中测试事务回滚。 As a result i do not want to use spring's AbstractTransactionalJUnit4SpringContextTests with the @Transactional annotation as that wraps my test method in a transaction. 结果,我不想将Spring的AbstractTransactionalJUnit4SpringContextTests@Transactional批注一起使用,因为它将我的测试方法包装在事务中。

I know spring also offers AbstractJUnit4SpringContextTests for tests without transactions. 我知道spring还提供了AbstractJUnit4SpringContextTests来进行没有事务的测试。 However i need to have some transactions to save data into my database and also query data for assertions after running the service under test. 但是,我需要进行一些事务以将数据保存到我的数据库中,并在运行被测服务后还要查询数据以获取断言。

How can i write a Junit 4 spring transactional test without the default transaction test management? 没有默认事务测试管理,如何编写Junit 4 spring事务测试?

Here is the answer to your question ; 这是您问题的答案;

@ContextConfiguration(value = "/applicationContext.xml")
public class JPASpringTest extends AbstractJUnit4SpringContextTests {
 @PersistenceContext(unitName="jpadenemelocal")
 EntityManager entityManager;

 @Autowired
 protected PlatformTransactionManager transactionManager;

 @Test
 public void testInsertRolManyToMany() {
 TransactionStatus status =  transactionManager.getTransaction(null);
 // your code   

 transactionManager.commit(status);
 }

}

The spring docs should cover this in their testing chapter 春季文档应在其测试章节中对此进行介绍

What you might want is to configure your test without the default TransactionalTestExecutionListener like this 您可能想要的是在没有默认TransactionalTestExecutionListener的情况下配置测试,如下所示

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class})
public class SimpleTest {

    @Test
    public void testMethod() {
        // execute test logic...
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 验证场景的事务性Spring Junit4测试用例 - Transactional Spring Junit4 test cases for Validation scenario Spring4 Junit4测试:@TransactionConfiguration VS @Transactional,仅使用一个还是一起使用? - Spring4 Junit4 Test: @TransactionConfiguration VS @Transactional, use only one or together? 如何将SpringJUnit4ClassRunner的'spring 3.1'面向junit4测试转换为基于spring的junit3.8测试? - How can I turn this 'spring 3.1' oriented junit4 test with SpringJUnit4ClassRunner into a spring oriented junit3.8 based test? 如何使Spring @Transactional在JUnit测试中工作 - How to make Spring @Transactional to work in junit test Spring JUnit4 Group集成测试 - Spring JUnit4 Group integration tests 如果使用Spring和Junit 4,如何编写内部测试类? 还是我还能如何构建测试? - How to write inner test classes if you use Spring and Junit 4? Or How else I can structure my tests? 如何在Spring2.5中使用JUnit4注释? - How do I get JUnit4 annotations to work in Spring2.5? 使用JUnit4进行测试时,Spring @transactional不会启动事务 - Spring @transactional does not start a transaction while testing with JUnit4 Spring / Hibernate(JUnit4)中的集成测试问题 - Integration Test issue in Spring/Hibernate (JUnit4) 如何在 Spring 中为事务方法编写事务单元测试? - How to write Transactional Unit Tests for Transactional Methods in Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM