Stub implementation of the core MongoDB server in Java. The MongoDB Wire Protocol is implemented with Netty. Different backends are possible and can be easily extended.
The in-memory backend is the default, such that mongo-java-server can be used as stub in unit tests. It does not support all features of the original MongoDB, and probably never will.
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.
Add the following Maven dependency to your project:
<dependency>
<groupId>de.bwaldvogel</groupId>
<artifactId>mongo-java-server</artifactId>
<version>1.1.1</version>
</dependency>
public class SimpleTest {
private DBCollection collection;
private MongoClient client;
private MongoServer server;
@Before
public void setUp() {
server = new MongoServer(new MemoryBackend());
// bind on a random local port
InetSocketAddress serverAddress = server.bind();
client = new MongoClient(new ServerAddress(serverAddress));
collection = client.getDB("testdb").getCollection("testcollection");
}
@After
public void tearDown() {
client.close();
server.shutdownNow();
}
@Test
public void testSimpleInsertQuery() throws Exception {
assertEquals(0, collection.count());
// creates the database and collection in memory and insert the object
DBObject obj = new BasicDBObject("_id", 1).append("key", "value");
collection.insert(obj);
assertEquals(1, collection.count());
assertEquals(obj, collection.findOne());
}
}
-
- shares the basic idea of implementing the wire protocol with Netty
- focus on in-memory backend for unit testing
-
- focus on unit testing
- no wire protocol implementation
- intercepts the java mongo driver
- currently used in nosql-unit