【发布时间】: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