MongoDb is a really good database. For a test addict like me it had one small problem. You either had to install a local mongo or use a distant mongo for your tests.
The first option is good because it doesn’t rely on the network. But building a java project should be as simple as: Buy a new computer, open a terminal, git clone [repo], mvn clean install. Done. (I guess a windows computer doesn’t qualify here…)
The second option is bad because tests shouldn’t rely on external resources. It’s slow and error-prone.
Fongo is an in-memory implementation of mongoDb. Not everything is implemented but it sufficient for most tests.
Here’s a test where I connect to an in-memory mongo, create and query documents. So simple!
To make it even easier to access mongo from Java code, I use Jongo. It rocks!
Notice also that to connect to a real mongo server, all I need is to change:
DB db = new Fongo("Test").getDB("Database");
DB db = new MongoClient(HOST).getDB("Database");