相关疑难解决方法(0)

如何在Spring Boot应用程序中配置嵌入式MongDB进行集成测试?

我有一个相当简单的Spring Boot应用程序,它公开一个小的REST API并从MongoDB的一个实例中检索数据.对MongoDB实例的查询通过基于Spring Data的存储库进行.下面的一些关键代码.

// Main application class
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan
@Import(MongoConfig.class)
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)
// Product repository with Spring data
public interface ProductRepository extends MongoRepository<Product, String> {

    Page<Product> findAll(Pageable pageable);

    Optional<Product> findByLineNumber(String lineNumber);
}
Run Code Online (Sandbox Code Playgroud)
// Configuration for "live" connections
@Configuration
public class MongoConfig {

    @Value("${product.mongo.host}")
    private String mongoHost;

    @Value("${product.mongo.port}")
    private String mongoPort;

    @Value("${product.mongo.database}")
    private String mongoDatabase;

    @Bean(name="mongoClient")
    public MongoClient mongoClient() throws IOException {
        return new MongoClient(mongoHost, Integer.parseInt(mongoPort));
    } …
Run Code Online (Sandbox Code Playgroud)

java spring mongodb spring-boot

25
推荐指数
4
解决办法
4万
查看次数

如何在集成测试中一起使用这些@DataMongoTest 和@SpringBootTest

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

@DataMongoTest 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MainControllerTest {
@LocalServerPort
    private int port = 8080;
/* some test cases*/ 
}
Run Code Online (Sandbox Code Playgroud)

但我得到以下错误

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)]
Run Code Online (Sandbox Code Playgroud)

看起来这两者是相互排斥的,那么如何进行集成测试。

junit integration-testing spring-boot-test

13
推荐指数
1
解决办法
6409
查看次数