我想对连接到React式mongodb的spring应用程序进行集成测试。但是经过测试,数据不会回滚。
mongo配置
@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-测试时,需要platformtransactionmanagerbean来测试事务管理。
但是reactivemongotransactionmanager没有实现。对于mongodb,只有mongotransactionmanager。我使用的是springboot2.4.4版本
然后我使用mongotransactionmanager尝试了另一个解决方案,但仍然不起作用。
这是我的测试配置
@TestConfiguration
@EnableTransactionManagement
public class MongoConfigurationIntegrationTest {
@Bean
@Primary
public PlatformTransactionManager mongoTransactionManager(MongoDatabaseFactory databaseFactory) {
return new MongoTransactionManager(databaseFactory);
}
}
使用MongoTransactionManager的新集成测试
@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();
}
}
控制台日志
o、 事务上下文:开始测试上下文的事务(1)[defaulttestcontext@36b4091c
org.mongodb.driver.connection:打开的连接[connectionid{l]ocalvalue:8,东南rvervalue:211}]至localhost:27017
o、 s.t.c.transaction.transactioncontext:用于测试的回滚事务:[defaulttestcontext@36b4091c
org.mongodb.driver.connection:关闭连接[connectionid{l]ocalvalue:5,东南rvervalue:208}]至localhost:27017
日志上说事务是回滚的,但是当我检查db时数据没有回滚。
配置有问题吗?
暂无答案!
目前还没有任何答案,快来回答吧!