Skip to content

Instantly share code, notes, and snippets.

@ggtools
Created October 7, 2014 06:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggtools/d62aeba970477cec3c52 to your computer and use it in GitHub Desktop.
Save ggtools/d62aeba970477cec3c52 to your computer and use it in GitHub Desktop.
A Spring configuration to use an embedded MongoDB during tests
@Configuration
public class TestMongoConfig {
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
private MongoProperties properties;
@Autowired(required = false)
private MongoClientOptions options;
@Bean(destroyMethod = "close")
public Mongo mongo() throws IOException {
Net net = mongod().getConfig().net();
properties.setHost(net.getServerAddress().getHostName());
properties.setPort(net.getPort());
return properties.createMongoClient(this.options);
}
@Bean(destroyMethod = "stop")
public MongodProcess mongod() throws IOException {
return mongodExe().start();
}
@Bean(destroyMethod = "stop")
public MongodExecutable mongodExe() throws IOException {
return starter.prepare(mongodConfig());
}
@Bean
public IMongodConfig mongodConfig() throws IOException {
return new MongodConfigBuilder().version(Version.Main.PRODUCTION).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment