我在 Spring Boot 中为 MongoRepository 编写了一个测试,测试工作正常。唯一的问题是,当测试结束时,我想要回滚,这样就不会因为测试导致数据库发生变化。
// 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()));
}
}
我将@Transactional 放在@Rollback 之前,但出现此错误:
java.lang.illegalstateexception:Failed to retrieve PlatformTransactionManager for @Transactional test for test context
当我搜索错误时,我找不到任何带有 MongoRepository 的代码。那么,我该如何解决呢?
编辑:添加后@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!