【问题标题】:How to use Embedded MongoDB on Spring Boot Integration tests如何在 Spring Boot 集成测试中使用嵌入式 MongoDB
【发布时间】:2016-08-26 04:31:27
【问题描述】:

我正在尝试使用 EmbeddedMongoDB 进行测试。

这是我的代码:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MyWebServiceApplication.class, TestMongoConfig.class})
public class MyWebServiceApplicationTests {

@Autowired 
InfoRepository repository;

@Before
public void setUp() {

}

@Test
public void setsIdOnSave() {
    Info dave = repository.findInfoByMobileNumber(917900);
    assertThat(dave.getCarrier(), is(notNullValue()));
    }
}

如果我删除 TestMongoConfig.class,测试可以工作,但它正在连接到实时数据库。

这是 TestMongoConfig 类。 I got this code here

@Configuration
public class TestMongoConfig {

@Autowired
private MongoProperties properties;

@Autowired(required = false)
private MongoClientOptions options;

@Bean(destroyMethod = "close")
public Mongo mongo(MongodProcess mongodProcess) throws IOException {
    Net net = mongodProcess.getConfig().net();
    properties.setHost(net.getServerAddress().getHostName());
    properties.setPort(net.getPort());
    return properties.createMongoClient(this.options);
}

@Bean(destroyMethod = "stop")
public MongodProcess mongodProcess(MongodExecutable mongodExecutable) throws IOException {
    return mongodExecutable.start();
}

@Bean(destroyMethod = "stop")
public MongodExecutable mongodExecutable(MongodStarter mongodStarter, IMongodConfig iMongodConfig) throws IOException {
    return mongodStarter.prepare(iMongodConfig);
}

@Bean
public IMongodConfig mongodConfig() throws IOException {
    return new MongodConfigBuilder().version(Version.Main.PRODUCTION).build();
}

@Bean
public MongodStarter mongodStarter() {
    return MongodStarter.getDefaultInstance();
}

}

现在,我收到此错误:

Could not autowire field: private org.springframework.boot.autoconfigure.mongo.MongoProperties com.example.TestMongoConfig.properties

请注意,我的应用程序连接到两个不同的 MongoDB 实例。我的“实时”连接配置没有使用自动配置,它有自己的 bean。 Here's my reference for achieving this.

【问题讨论】:

    标签: mongodb spring-boot


    【解决方案1】:
    @Configuration
    @EnableAutoConfiguration
    @EnableMongoRepositories(basePackages = { "package1", "package2" })
    public class TestMongoConfig {
    
    @Autowired
    private MongoProperties properties;
    
    @Autowired(required = false)
    private MongoClientOptions options;
    
    @Bean(destroyMethod = "close")
    public Mongo mongo(MongodProcess mongodProcess) throws IOException {
        Net net = mongodProcess.getConfig().net();
        properties.setHost(net.getServerAddress().getHostName());
        properties.setPort(net.getPort());
        return properties.createMongoClient(this.options);
    }
    
    @Bean(destroyMethod = "stop")
    public MongodProcess mongodProcess(MongodExecutable mongodExecutable) throws IOException {
        return mongodExecutable.start();
    }
    
    @Bean(destroyMethod = "stop")
    public MongodExecutable mongodExecutable(MongodStarter mongodStarter, IMongodConfig iMongodConfig) throws IOException {
        return mongodStarter.prepare(iMongodConfig);
    }
    
    @Bean
    public IMongodConfig mongodConfig() throws IOException {
         return new MongodConfigBuilder().version(Version.Main.PRODUCTION)
                .cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(true)
                        .useStorageEngine("mmapv1")
             .build())
                .build(); 
    }
    
    @Bean
    public MongodStarter mongodStarter() {
        return MongodStarter.getDefaultInstance();
    }
    
    }
    

    @EnableAutoConfiguration@EnableMongoRepositories(basePackages = { "package1", "package2" }) 需要在那里。我猜这就是 Spring Boot 将嵌入式 MongoDB 集成到它的 CrudRepository 中的方式

    接下来是这段代码:

         return new MongodConfigBuilder().version(Version.Main.PRODUCTION)
                .cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(true)
                        .useStorageEngine("mmapv1")
             .build())
                .build();
    

    这是我在 32 位机器上运行的代码。我必须设置 mmapv1 并设置 useNoJournal 来修复 无法启动进程:EOF 错误

    对于错误

    Could not autowire field: private org.springframework.boot.autoconfigure.mongo.MongoProperties com.example.TestMongoConfig.properties
    

    我必须创建一个 application.properties,其中包含我在主要代码中的相同内容。

    【讨论】:

    • @vinothkumar 是的,它解决了我的问题。但我已经将我的操作系统和 mongodb 升级到 64 位。
    • 对我来说,它是从 app-context.xml 获取实际配置
    • 啊...就我而言,我使用 Spring Boot 并且不得不避免使用 xml 配置文件。所以我必须使用的大部分东西都是注释
    • 这是我发布的答案。您需要哪方面的帮助?
    猜你喜欢
    • 2018-10-09
    • 2017-01-22
    • 2020-09-05
    • 2018-07-04
    • 2020-12-11
    • 1970-01-01
    • 2020-07-01
    • 2014-12-12
    • 1970-01-01
    相关资源
    最近更新 更多