简体   繁体   English

Spring Boot MongoRepository @Rollback 用于测试

[英]Spring Boot MongoRepository @Rollback for tests

I wrote a Test for a MongoRepository in Spring Boot, and the test works fine.我在 Spring Boot 中为 MongoRepository 编写了一个测试,测试工作正常。 The only problem is that when the test is over, I want a rollback, so that there will be no change in the database caused by the test.唯一的问题是,当测试结束时,我想要回滚,这样就不会因为测试而导致数据库发生变化。

// package...

// imports...

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MetistrafficApplication.class)
@Rollback(true)
public class AppRepositoryTests {

    @Autowired
    private AppRepository appRepository;

    @Test
    public void insertTest() {
        App app = new App("test");
        App appInserted = appRepository.save(app);

        assertThat(appInserted.getName(), equalTo(app.getName()));
    }
}

I put @Transactional before @Rollback, but get this error:我把@Transactional 放在@Rollback 之前,但得到这个错误:

java.lang.illegalstateexception:Failed to retrieve PlatformTransactionManager for @Transactional test for test context

When I searched for the error, I couldn't find any code with MongoRepository.当我搜索错误时,我找不到任何带有 MongoRepository 的代码。 So, how can I solve this?那么,我该如何解决这个问题?

EDIT: After adding @Transactional("PlatformTransactionManager") , the error I get is changed to this:编辑:添加@Transactional("PlatformTransactionManager") ,我得到的错误更改为:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'PlatformTransactionManager' is defined: No matching PlatformTransactionManager bean found for qualifier 'PlatformTransactionManager' - neither qualifier match nor bean name match!

As far as I know, there isn't an implementation of Spring's TransactionManager for MongoDB since it is not transactional in the ACID sense.据我所知,没有 Spring 的 TransactionManager for MongoDB 实现,因为它在 ACID 意义上不是事务性的。 So no, you cannot use @Transactional annotations with MongoDB and you'll have to do all the cleanup manually or else use DBUnit and add your own extensions for MongoDB.所以不,您不能在 MongoDB 中使用 @Transactional 注释,您必须手动完成所有清理工作,或者使用 DBUnit 并为 MongoDB 添加您自己的扩展。

EDIT: As Petter mentioned in his answer, starting with MongoDB 4.0, MongoDB has support for ACID transactions and you can find the official SpringData examples on GitHub and also have the feature's release post in Spring's developer blog编辑:正如Petter在他的回答中提到的,从 MongoDB 4.0 开始,MongoDB支持 ACID 事务,您可以在 GitHub 上找到官方 SpringData 示例,并且在 Spring 的开发者博客中也有该功能的发布帖子

Now you can use @Transactional with mongo.现在您可以在 mongo 中使用 @Transactional。 Take a look at this example: https://www.baeldung.com/spring-data-mongodb-transactions看看这个例子: https : //www.baeldung.com/spring-data-mongodb-transactions

You'll need mongo 4.0.你需要 mongo 4.0。 Also need to enable mongo replication ( mongod --replSet rs0 )还需要启用 mongo 复制( mongod --replSet rs0

Then you'll need to add this bean to your spring application然后你需要把这个 bean 添加到你的 spring 应用程序中

@Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
    return new MongoTransactionManager(dbFactory);
}

This is enough to use @Transactional in your code.这足以在您的代码中使用@Transactional

I guess you use try catch block.我猜你用的是 try catch 块。 Its better if you can avoid try catch.如果您可以避免尝试捕获,那就更好了。 Anyway if you need to rollback you can do it like this.无论如何,如果您需要回滚,您可以这样做。

 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

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

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