简体   繁体   English

Junit忽略服务中的@Transactional批注

[英]Junit Ignores my @Transactional annotation in services

We have spring services in our project. 我们在项目中提供春季服务。 for example UserService. 例如UserService。

These services use the @Transactional annotation. 这些服务使用@Transactional批注。

We just tried to execute junit tests that involves these services and for some reason the @Transactional annotations are ignored. 我们只是试图执行涉及这些服务的junit测试,并且出于某种原因,@ Transactional批注将被忽略。 (For example, i use a @Test without @Transactional , it goes to a service that is indeed anotated @Transactional, in that service it saves a new object(entity) the entity is being saved in the DB before the end of the transaction (Commit).) (例如,我使用不带@Transactional的@Test,它转到确实注释为@Transactional的服务,在该服务中,它保存实体的新对象(实体),该实体将在事务结束之前保存在数据库中(承诺)。)

I would like to point out that we don't want our junit tests to be annotated with @Transactional themselves. 我想指出,我们不希望我们的junit测试使用@Transactional本身进行注释。

Is it possible to enforce junit to not ignore the @Transactional annotations in our services? 是否可以强制junit不忽略我们服务中的@Transactional注释?

Thanks, 谢谢,

You should explicitly tell JUnit to use transactions (example from reference): 您应该明确地告诉JUnit使用事务(参考中的示例):

@ContextConfiguration(locations={"example/test-context.xml"})
@TransactionConfiguration(transactionManager="txMgr", defaultRollback=false)
public class CustomConfiguredTransactionalTests {
    // class body...
}

Then you can use @NonTransactional/@Rollback/etc annotations in order to control the behavior for particular methods. 然后,您可以使用@ NonTransactional / @ Rollback / etc批注来控制特定方法的行为。

Manual configuration is required as previously mentioned, as transactions are not persisted in Spring test contexts. 如前所述,需要手动配置,因为事务不会在Spring测试上下文中持久存在。

Spring reference for this topic can be found here 有关此主题的春季参考,可以在这里找到

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM