Fake implementation of the core MongoDB server in Java that can be used for integration tests.
Think of H2/HSQLDB/SQLite but for MongoDB.
The MongoDB Wire Protocol is implemented with Netty. Different backends are possible and can be extended.
The in-memory backend is the default backend that is typically used to fake MongoDB for integration tests. It supports most CRUD operations, commands and the aggregation framework. Some features are not yet implemented, such as transactions, full-text search or map/reduce.
Add the following Maven dependency to your project:
<dependency>
<groupId>de.bwaldvogel</groupId>
<artifactId>mongo-java-server</artifactId>
<version>1.38.0</version>
</dependency>
public class SimpleTest {
private MongoCollection<Document> collection;
private MongoClient client;
private MongoServer server;
@Before
public void setUp() {
server = new MongoServer(new MemoryBackend());
// optionally:
// server.enableSsl(key, keyPassword, certificate);
// server.enableOplog();
// bind on a random local port
InetSocketAddress serverAddress = server.bind();
client = new MongoClient(new ServerAddress(serverAddress));
collection = client.getDatabase("testdb").getCollection("testcollection");
}
@After
public void tearDown() {
client.close();
server.shutdown();
}
@Test
public void testSimpleInsertQuery() throws Exception {
assertEquals(0, collection.count());
// creates the database and collection in memory and insert the object
Document obj = new Document("_id", 1).append("key", "value");
collection.insertOne(obj);
assertEquals(1, collection.count());
assertEquals(obj, collection.find().first());
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes={SimpleSpringBootTest.TestConfiguration.class})
public class SimpleSpringBootTest {
@Autowired private MyRepository repository;
@Before
public void setUp() {
// initialize your repository with some test data
repository.deleteAll();
repository.save(...);
}
@Test
public void testMyRepository() {
// test your repository ...
...
}
@Configuration
@EnableMongoTestServer
@EnableMongoRepositories(basePackageClasses={MyRepository.class})
protected static class TestConfiguration {
// test bean definitions ...
...
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(MongoTestServerConfiguration.class)
public @interface EnableMongoTestServer {
}
public class MongoTestServerConfiguration {
@Bean
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) {
return new MongoTemplate(mongoDbFactory);
}
@Bean
public MongoDbFactory mongoDbFactory(MongoServer mongoServer) {
InetSocketAddress serverAddress = mongoServer.getLocalAddress();
return new SimpleMongoClientDbFactory("mongodb://" + serverAddress.getHostName() + ":" + serverAddress.getPort() + "/test");
}
@Bean(destroyMethod = "shutdown")
public MongoServer mongoServer() {
MongoServer mongoServer = new MongoServer(new MemoryBackend());
mongoServer.bind();
return mongoServer;
}
}
The H2 MVStore backend connects the server to a MVStore
that
can either be in-memory or on-disk.
<dependency>
<groupId>de.bwaldvogel</groupId>
<artifactId>mongo-java-server-h2-backend</artifactId>
<version>1.38.0</version>
</dependency>
public class Application {
public static void main(String[] args) throws Exception {
MongoServer server = new MongoServer(new H2Backend("database.mv"));
server.bind("localhost", 27017);
}
}
The PostgreSQL backend is a proof-of-concept implementation that connects the server to a database in a running PostgreSQL 9.5+ instance. Each MongoDB database is mapped to a schema in Postgres and each MongoDB collection is stored as a table.
<dependency>
<groupId>de.bwaldvogel</groupId>
<artifactId>mongo-java-server-postgresql-backend</artifactId>
<version>1.38.0</version>
</dependency>
public class Application {
public static void main(String[] args) throws Exception {
DataSource dataSource = new org.postgresql.jdbc3.Jdbc3PoolingDataSource();
dataSource.setDatabaseName(…);
dataSource.setUser(…);
dataSource.setPassword(…);
MongoServer server = new MongoServer(new PostgresqlBackend(dataSource));
server.bind("localhost", 27017);
}
}
If you want to build a version that is not on Maven Central you can do the following:
- Build a "fat" JAR that includes all dependencies using "
./gradlew shadowJar
" - Copy
build/libs/mongo-java-server-[version]-all.jar
to your project, e.g. to thelibs
directory. - Import that folder (e.g. via Gradle using
testCompile fileTree(dir: 'libs', include: '*.jar')
)
Please read the contributing guidelines if you want to contribute code to the project.
If you want to thank the author for this library or want to support the maintenance work, we are happy to receive a donation.
A faulty backend could randomly fail queries or cause timeouts. This could be used to test the client for error resilience.
Fuzzing the wire protocol could be used to check the robustness of client drivers.
Please note that transactions are currently not supported. Please see the discussion in issue #143.
When using mongo-java-server
for integration tests, you can use Testcontainers or Embedded MongoDB instead to spin-up a real MongoDB that will have full transaction support.
-
- Can be used to spin-up a real MongoDB instance in a Docker container
-
- Spins up a real MongoDB instance
-
- focus on unit testing
- no wire protocol implementation
- intercepts the java mongo driver
- currently used in nosql-unit