3 import java.io.IOException;
4 import java.net.BindException;
5 import java.net.ServerSocket;
7 import org.junit.rules.ExternalResource;
9 import de.flapdoodle.embed.mongo.config.IMongodConfig;
10 import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
11 import de.flapdoodle.embed.mongo.config.Net;
12 import de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion;
13 import de.flapdoodle.embed.mongo.distribution.Version;
14 import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
17 * JUnit rule that creates an in-memory instance of MongoDB using the flapdoodle Embedded MongoDB.
19 public class EmbeddedMongo extends ExternalResource {
21 private MongodForTestsFactory mongod;
24 protected void before() throws Throwable {
26 mongod = new MongodFactory(); // starts process in constructor
30 protected void after() {
36 private void requirePort() throws IOException, IllegalStateException {
37 try (ServerSocket ignore = new ServerSocket(27017)) {
39 } catch (BindException e) {
40 throw new IllegalStateException("Could not bind port 27017 which is necessary to run the MongoDB tests", e);
44 public static class MongodFactory extends MongodForTestsFactory {
46 public MongodFactory() throws IOException {
47 super(Version.Main.V3_5);
51 protected IMongodConfig newMongodConfig(IFeatureAwareVersion version) throws IOException {
52 return new MongodConfigBuilder()
53 .net(new Net(27017, false))