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

2024-05-24

我有一个相当简单的 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);
    }
}
// Product repository with Spring data
public interface ProductRepository extends MongoRepository<Product, String> {

    Page<Product> findAll(Pageable pageable);

    Optional<Product> findByLineNumber(String lineNumber);
}
// 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));
    }

    @Autowired
    @Bean(name="mongoDbFactory")
    public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
        return new SimpleMongoDbFactory(mongoClient, mongoDatabase);
    }

    @Autowired
    @Bean(name="mongoTemplate")
    public MongoTemplate mongoTemplate(MongoClient mongoClient) {
        return new MongoTemplate(mongoClient, mongoDatabase);
    }
}
@Configuration
@EnableMongoRepositories
public class EmbeddedMongoConfig {

    private static final String DB_NAME = "integrationTest";
    private static final int DB_PORT = 12345;
    private static final String DB_HOST = "localhost";
    private static final String DB_COLLECTION = "products";

    private MongodExecutable mongodExecutable = null;

    @Bean(name="mongoClient")
    public MongoClient mongoClient() throws IOException {
        // Lots of calls here to de.flapdoodle.embed.mongo code base to 
        // create an embedded db and insert some JSON data
    }

    @Autowired
    @Bean(name="mongoDbFactory")
    public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
        return new SimpleMongoDbFactory(mongoClient, DB_NAME);
    }

    @Autowired
    @Bean(name="mongoTemplate")
    public MongoTemplate mongoTemplate(MongoClient mongoClient) {
        return new MongoTemplate(mongoClient, DB_NAME);
    }

    @PreDestroy
    public void shutdownEmbeddedMongoDB() {
        if (this.mongodExecutable != null) {
            this.mongodExecutable.stop();
        }
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestProductApplication.class)
@IntegrationTest
@WebAppConfiguration
public class WtrProductApplicationTests {

    @Test
    public void contextLoads() {
        // Tests empty for now
    }

}
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan
@Import(EmbeddedMongoConfig.class)
public class TestProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestProductApplication.class, args);
    }
}

因此,这里的想法是让集成测试(目前为空)连接到嵌入式 mongo 实例,而不是“实时”实例。然而,这不起作用。我可以看到连接到 Mongo 的“实时”实例的测试,如果我关闭它,构建就会失败,因为它仍在尝试连接到 Mongo 的实时实例。有人知道为什么吗?如何获取连接到嵌入式实例的测试?


从 Spring Boot 1.3 版本开始,有一个EmbeddedMongoAutoConfiguration开箱即用的类。这意味着您根本不必创建配置文件,并且如果您想更改某些内容仍然可以。

添加了嵌入式 MongoDB 的自动配置。依赖于de.flapdoodle.embed:de.flapdoodle.embed.mongo这就是开始所需的全部内容。配置,例如要使用的 Mongo 版本,可以通过 application.properties 进行控制。请参阅文档以获取更多信息。 (Spring Boot 发行说明 https://github.com/spring-projects/spring-boot/wiki/spring-boot-1.3-release-notes)

必须添加到 application.properties 文件中的最基本和最重要的配置是
spring.data.mongodb.port=0(0表示从免费的中随机抽取)

欲了解更多详情,请检查:Spring Boot 文档 MongoDb http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html#boot-features-mongo-embedded

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何配置嵌入式 MongoDB 以在 Spring Boot 应用程序中进行集成测试? 的相关文章

随机推荐