0

I am trying to use EmbeddedMongoDB to work on my tests.

Here are my codes:

@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()));
    }
}

If I remove TestMongoConfig.class, the test works but it is connecting to the live DB.

Here's the TestMongoConfig class. 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();
}

}

Now, I am getting this error:

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

Please note that my application is connecting to two different MongoDB instances. My configuration for the "live" connection isn't using Autoconfiguration and it has its own beans. Here's my reference for achieving this.

Community
  • 1
  • 1
Francis Zabala
  • 1,037
  • 2
  • 11
  • 30

1 Answers1

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 and @EnableMongoRepositories(basePackages = { "package1", "package2" }) needs to be there. I am guessing that it's how Spring Boot integrates embedded MongoDB into it's CrudRepository

Next is this code:

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

That is my code as I am running this in a 32 bit machine. I had to set mmapv1 and set useNoJournal to fix the Could not start process: EOF error I am having.

For the error

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

I have to create an application.properties containing the same stuff I have in the main codes.

Francis Zabala
  • 1,037
  • 2
  • 11
  • 30