[英]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.