In our project we use the Gradle Build Tool and the Spring Framework to access a MongoDB.
For integration testing we decided to use Fongo. Let's add Fongo to the test dependencies first:
dependencies {
testCompile "com.github.fakemongo:fongo:${fongoVersion}"
}
Next we override the Mongo
bean in the profile fongo
.
@ComponentScan
@Profile("fongo")
public class FongoConfiguration extends AbstractMongoConfiguration {
@Override
protected String getDatabaseName() {
return "fongo-test-db";
}
@Override
@Bean
public Mongo mongo() throws Exception {
return new Fongo("Fongo").getMongo();
}
}
With this groundwork done it's easy to setup an integration test for our repository.
We activate the profile fongo
and the FongoConfiguration
via annotations to our test classes.
@ActiveProfiles("fongo")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {FongoConfiguration.class})
public class MyRepositoryTest {
....
}
When running the fongo
tests you should be able to spot something like...
15:50:33.120 [main] DEBUG osdmc.MongoDbUtils - Getting Mongo Database name=[fongo-test-db]
in your logs.