Skip to content

Latest commit

History

519 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

fongo

Fongo is an in-memory java implementation of mongo. It intercepts calls to the standard mongo-java-driver for finds, updates, inserts, removes and other methods. The primary use is for lightweight unit testing where you don't want to spin up a mongo process.

Usage

Add dependency to your project:

<dependency>
<groupId>com.github.fakemongo</groupId>
<artifactId>fongo</artifactId>
<version>1.3.7</version>
<scope>test</scope>
</dependency>

Alternatively: clone this repo and build the jar: mvn package then copy jar to your classpath

Use in place of regular com.mongodb.Mongo instance:

importcom.github.fakemongo.Fongo;
importcom.mongodb.BasicDBObject;
importcom.mongodb.DB;
importcom.mognodb.DBCollection;
...
Fongofongo = newFongo("mongo server 1");
// once you have a DB instance, you can interact with it// just like you would with a real one.DBdb = fongo.getDB("mydb");
DBCollectioncollection = db.getCollection("mycollection");
collection.insert(newBasicDBObject("name", "jon"));

Scope

fongo doesn't implement all mongo functionality. most query and update syntax is supported. Gridfs and capped collections are not supported. MapReduce is in minimal way but will be enhanced soon.

Implementation Details

Fongo depends on Objenesis to hijack the com.mongodb.MongoClient class. It has a "provided" dependency on the mongo-java-driver and was tested with 2.11.3. It also has a "provided" dependency on sl4j-api for logging. If you don't already have sl4j in your project, you can add a maven dependency to the logback implementation like this:

<dependency> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
<scope>test</test>
</dependency>

Fongo should be thread safe. All read and write operations on collections are synchronized. It's pretty course, but should be good enough for simple testing. Fongo doesn't have any shared state (no statics). Each Fongo instance is completely independent.

Usage Details

// Fongo instance methods// get all created databases (they are created automatically the first time requested)Collection<DB> dbs = fongo.getUsedDatabases();
// alsoList<String> dbNames = fongo.getDatabaseNames();
// alsofongo.dropDatabase("dbName");
// get an instance of the hijacked com.mongodb.MongoMongomongo = fongo.getMongo();

If you use Spring, you can configure fongo in your XML configuration context:

<beanname="fongo"class="com.github.fakemongo.Fongo">
<constructor-argvalue="InMemoryMongo" />
</bean>
<beanid="mongo"factory-bean="fongo"factory-method="getMongo" />
<mongo:db-factoryid="mongoDbFactory"mongo-ref="mongo" />
<!-- localhost settings for mongo --><!--<mongo:db-factory id="mongoDbFactory" />-->
<beanid="mongoTemplate"class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-argref="mongoDbFactory"/>
</bean>

Junit

If you use JUnit in your project, you can use Rule to instanciate a Fongo object :

@RulepublicFongoRulefongoRule = newFongoRule();

If you need, you can easely swith to your real MongoDB server (on localhost for now).

@RulepublicFongoRulefongoRule = newFongoRule(true);

WARNING : in this case, the database WILL BE DROPPED when test is finish. So, use UUID, random database, BUT NOT your real database.

Todo

  • more testing
  • complete compatibility with Jongo

Reporting Bugs and submitting patches

If you discover a bug with fongo you can file an issue in github. At the very least, please include a complete description of the problem with steps to reproduce. If there were exceptions thrown, please include the complete stack traces. If it's not easy to explain the problem, failing test code would be helpful. You can fork the project and add a new failing test case.

It's even better if you can both add the test case and fix the bug. I will merge pull requests with test cases and add your name to the patch contributers below. Please maintain the same code formatting and style as the rest of the project.

Original Author

Patch Contributers

About

faked out in-memory mongo for java

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages