fongo

Fake in-memory mongo

License

License

GroupId

GroupId

com.github.krraghavan
ArtifactId

ArtifactId

fongo
Last Version

Last Version

2.0.14
Release Date

Release Date

Type

Type

jar
Description

Description

fongo
Fake in-memory mongo
Project URL

Project URL

https://github.com/krraghavan/fongo
Source Code Management

Source Code Management

https://github.com/krraghavan/fongo/tree/master

Download fongo

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.krraghavan/fongo/ -->
<dependency>
    <groupId>com.github.krraghavan</groupId>
    <artifactId>fongo</artifactId>
    <version>2.0.14</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.krraghavan/fongo/
implementation 'com.github.krraghavan:fongo:2.0.14'
// https://jarcasting.com/artifacts/com.github.krraghavan/fongo/
implementation ("com.github.krraghavan:fongo:2.0.14")
'com.github.krraghavan:fongo:jar:2.0.14'
<dependency org="com.github.krraghavan" name="fongo" rev="2.0.14">
  <artifact name="fongo" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.krraghavan', module='fongo', version='2.0.14')
)
libraryDependencies += "com.github.krraghavan" % "fongo" % "2.0.14"
[com.github.krraghavan/fongo "2.0.14"]

Dependencies

compile (4)

Group / Artifact Type Version
org.mozilla : rhino jar 1.7.7.1
org.objenesis : objenesis Optional jar 2.3
com.vividsolutions : jts jar 1.13
de.grundid.opendatalab : geojson-jackson jar 1.2

provided (3)

Group / Artifact Type Version
org.mongodb : mongo-java-driver jar 3.4.1
org.slf4j : slf4j-api jar 1.7.21
junit : junit jar 4.12

test (15)

Group / Artifact Type Version
org.assertj : assertj-core jar 1.6.1
ch.qos.logback : logback-classic jar 1.1.7
org.jongo : jongo jar 1.2
org.springframework : spring-test jar 4.2.6.RELEASE
org.springframework.data : spring-data-mongodb jar 1.9.1.RELEASE
org.springframework.hateoas : spring-hateoas jar 0.19.0.RELEASE
cglib : cglib jar 3.1
com.google.guava : guava jar 19.0
org.scalatest : scalatest_2.11 jar 2.2.3
org.mongodb : casbah_2.11 pom 3.1.1
org.scala-lang : scala-library jar 2.11.6
org.scala-lang : scala-compiler jar 2.11.6
org.openjdk.jmh : jmh-core jar 1.12
org.openjdk.jmh : jmh-generator-annprocess jar 1.12
org.apache.commons : commons-lang3 jar 3.3.2

Project Modules

There are no modules declared in this project.

Build Status

fongo

Fongo is an in-memory java implementation of MongoDB. 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 mongod process.

Maven Central License Apache2

Usage

Add dependency to your project:

If you use 3.X drivers

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

Other dependency management

If you use 2.X drivers (this branch fongo-drivers-2.x) will be deprecated soon

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

Other dependency management

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:

import com.github.fakemongo.Fongo;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mognodb.DBCollection;
...
Fongo fongo = new Fongo("mongo server 1");

// once you have a DB instance, you can interact with it
// just like you would with a real one.
DB db = fongo.getDB("mydb");
DBCollection collection = db.getCollection("mycollection");
collection.insert(new BasicDBObject("name", "jon"));

Scope

Fongo doesn't implement all MongoDB 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.

  • $near can be used
  • $geoWithin can be used with $box for now.

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.13.0 and 3.0.1. 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</scope>
</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();
// also
List<String> dbNames = fongo.getDatabaseNames();
// also
fongo.dropDatabase("dbName");

// get an instance of the hijacked com.mongodb.Mongo
Mongo mongo = fongo.getMongo();

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

<bean name="fongo" class="com.github.fakemongo.Fongo">
    <constructor-arg value="InMemoryMongo" />
</bean>
<bean id="mongo" factory-bean="fongo" factory-method="getMongo" />

<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" />

<!-- localhost settings for mongo -->
<!--<mongo:db-factory id="mongoDbFactory" />-->

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongoDbFactory"/>
</bean>

Junit

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

@Rule
public FongoRule fongoRule = new FongoRule();

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

@Rule
public FongoRule fongoRule = new FongoRule(true);

WARNING : In this case, the database WILL BE DROPPED when test is finish. So, use a random database name (e.g. UUID), BUT NOT your real database.

You can specify the version of the database with :

@Rule
public FongoRule fongoRule = new FongoRule(new ServerVersion(2, 6));

In this case, the drivers didn't handle queries with the same way.

Text Search Simulation

Fongo simulates text search now. The results of text search are quite similar to real, but not exactly.

Next features are supported:

  • Plain words search
  • Search strings
  • Negated words
  • Projections in search query
  • Limits

Fongo text search simulation does not support:

  • Languages (including language-specific stop words)
  • Filter (maybe in future)
  • Weights in text index (we plan to support them in future)

Limitations, Differences:

  • Only text command search is supported. We will support find query with $text operator probably in future.
  • Scores in returned results are not always the same as the real Mongo's scores.
  • Only one field can be indexed as text field now. This limitation will be removed soon.

Usage example of the text search simulation:

    @Test
    public void findByTextTest() {
    //....
    //Define index:
    collection.createIndex(new BasicDBObject("myTextFieldToIndex", "text"));

    DBObject textSearchCommand = new BasicDBObject("search", "my search \"my phrase\" -without -this -words");

    //Search Command
    DBObject textSearchResult = collection.getDB()
            .command(new BasicDBObject("collection", new BasicDBObject("text", textSearchCommand)));

    //Make your assertions
    //....
	}

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 contributors below. Please maintain the same code formatting and style as the rest of the project.

Changelog

Version 2.0.0 break compatibility with 2.X driver version. Version 1.6.0 break compatibility with 2.12.X driver version.

Original Author

Patch Contributers

Versions

Version
2.0.14
2.0.13