0

我想对连接到响应式 mongodb 的 spring 应用程序进行集成测试。但经过测试,数据并没有回滚。

蒙戈配置

@AllArgsConstructor
@Configuration
@EnableTransactionManagement
@EnableReactiveMongoRepositories
public class MongoConfiguration extends AbstractReactiveMongoConfiguration {

    private MongoProperties mongoProperties;
    
    @Bean
    public ReactiveMongoTransactionManager transactionManager(ReactiveMongoDatabaseFactory dbFactory) {
        return new ReactiveMongoTransactionManager(dbFactory);
    }

    @Override
    protected String getDatabaseName() {
        return mongoProperties.getDatabase();
    }

    @Override
    protected void configureClientSettings(Builder builder) {
        ConnectionString connectionString = new ConnectionString(mongoProperties.getUri());
        builder.applyConnectionString(connectionString);
    }

    @Override
    protected boolean autoIndexCreation() {
        return mongoProperties.isAutoIndexCreation();
    }

    @Override
    protected void configureConverters(MongoConverterConfigurationAdapter converterConfigurationAdapter) {
        converterConfigurationAdapter.registerConverter(new OffsetDateTimeReadConverter());
        converterConfigurationAdapter.registerConverter(new OffsetDateTimeWriteConverter());
    }

}

这是我的示例服务

@RequiredArgsConstructor
@Service
public class SampleServiceImpl implements SampleService {

    @NonNull
    private SampleRepository sampleRepository;

    @Transactional
    public Mono<Sample> save(Sample sample) {
        return sampleRepository.save(sample);
    }

}

集成测试

@SpringBootTest
@ActiveProfiles("test")
public class SampleServiceTest {
   
    @Autowired
    private SampleService sampleService;

    @Rollback
    @Test
    public void saveTest() {
        Sample sample = new Sample();
        sampe.setName("Test");
        Mono<Sample> resultMono = sampleService.save(sample);
        StepVerifier.create(resultMono)
            .consumeNextWith(result -> {
                assertThat(result, is(samePropertyValuesAs(sample, "id")));
            })
            .verifyComplete();
    }

}

测试运行成功,但数据没有回滚

我在https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#integration-testing上阅读了 spring 测试文档,它需要 PlatformTransactionManager bean 来测试事务管理。

但是 ReactiveMongoTransactionManager 没有实现。对于 mongodb,只有 MongoTransactionManager。我正在使用 Spring Boot 版本 2.4.4

然后我尝试使用 MongoTransactionManager 的另一个解决方案,但仍然没有工作。

这是我的测试配置

@TestConfiguration
@EnableTransactionManagement
public class MongoConfigurationIntegrationTest {

    @Bean
    @Primary
    public PlatformTransactionManager mongoTransactionManager(MongoDatabaseFactory databaseFactory) {
        return new MongoTransactionManager(databaseFactory);
    }

}

使用 MongoTransacationManager 的新集成测试

@SpringBootTest
@ActiveProfiles("test")
@ContextConfiguration(classes = MongoConfigurationIntegrationTest.class)
public class SampleServiceTest {
   
    @Autowired
    private SampleService sampleService;

    @Transactional
    @Rollback
    @Test
    public void saveTest() {
        Sample sample = new Sample();
        sampe.setName("Test");
        Mono<Sample> resultMono = sampleService.save(sample);
        StepVerifier.create(resultMono)
            .consumeNextWith(result -> {
                assertThat(result, is(samePropertyValuesAs(sample, "id")));
            })
            .verifyComplete();
    }

}

控制台日志

ostctransaction.TransactionContext : 开始测试上下文的事务 (1) [DefaultTestContext@36b4091c

org.mongodb.driver.connection : 打开连接 [connectionId{localValue:8, serverValue:211}] 到 localhost:27017

ostctransaction.TransactionContext :回滚事务以进行测试:[DefaultTestContext@36b4091c

org.mongodb.driver.connection :关闭连接 [connectionId{localValue:5, serverValue:208}] 到 localhost:27017

日志说事务是回滚的,但是当我检查数据库时数据没有回滚。

我是不是配置有问题?

4

0 回答 0