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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@vinothkumar yes, it solved my issue. But I have upgrade my os and mongodb to 64 bit.
For me it is taking the actual configuration from app-context.xml
Ah... For my case, I use Spring Boot and had to avoid xml configuration files. So most of the stuff I have to use is annotations
It's on the answer I posted. What part do you need help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.