如何在Spring Boot Integration测试中使用Embedded MongoDB

时间:2016-05-01 09:13:48

标签: mongodb spring-boot

我正在尝试使用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实例。我对" live"的配置连接不是使用自动配置,它有自己的bean。 Here's my reference for achieving this.

1 个答案:

答案 0 :(得分:0)

@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,其中包含我在主代码中所拥有的相同内容。