Junit 如何在集成测试中同时使用@DataMongoTest和@SpringBootTest

Junit 如何在集成测试中同时使用@DataMongoTest和@SpringBootTest,junit,integration-testing,spring-boot-test,Junit,Integration Testing,Spring Boot Test,我正在尝试为我的一个rest应用程序编写集成测试用例,该应用程序在内部使用mongodb来持久化数据 @DataMongoTest @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class MainControllerTest { @LocalServerPort private int port = 8080; /* some test cases*/ } 但我的错误率越来越低 java.l

我正在尝试为我的一个rest应用程序编写集成测试用例,该应用程序在内部使用mongodb来持久化数据

@DataMongoTest 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MainControllerTest {
@LocalServerPort
    private int port = 8080;
/* some test cases*/ 
}
但我的错误率越来越低

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.sample.core.controller.MainControllerTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]

看起来这两者是互斥的,因此如何进行集成测试。

将@AutoConfigureDataMongo与@SpringBootTest一起使用,这将解决这个不明确的问题@SpringBootTest和@DataMongoTest不能一起使用。

请参阅此问题、注释和答案:此问题可能重复,我的问题已解决: