Skip to content

Instantly share code, notes, and snippets.

@mgandin
Last active June 7, 2019 15:54
Show Gist options
  • Save mgandin/6869335 to your computer and use it in GitHub Desktop.
Save mgandin/6869335 to your computer and use it in GitHub Desktop.
How to embed MongoDb for your Junit Integration test
import java.io.IOException;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
public class EmbeddedMongoDb {
private int port;
private MongodProcess process;
public EmbeddedMongoDb() {
this.port = 27017;
}
public EmbeddedMongoDb(int port) {
this.port = port;
}
public void start() throws IOException {
MongodStarter starter = MongodStarter.getDefaultInstance();
MongodExecutable executable = starter.prepare(new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(port,Network.localhostIsIPv6()))
.build());
process = executable.start();
}
public void stop() {
process.stop();
}
}
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class MongoTest {
private EmbeddedMongo embeddedMongo;
@Autowired
private MongoTemplate mongoTemplate;
@Before public void setUp() throws Exception {
embeddedMongo = new EmbeddedMongo();
embeddedMongo.start();
}
@Test public void should_write_in_mongo() {
Assert.assertNotNull(mongoTemplate);
A a = new A();
a.id = "1";
mongoTemplate.save(a);
A b = mongoTemplate.findAll(A.class).get(0);
Assert.assertEquals("1",b.id);
}
@After public void tearDown() {
embeddedMongo.stop();
}
public static class A {
String id;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="localhost" port="27017"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="test"/>
</bean>
</beans>
@rpringadi
Copy link

The EmbeddedMongoDb.java should be EmbeddedMongo.java, right? and the class name also need to change from EmbeddedMongoDb to EmbeddedMongo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment