Integration Testing with MongoDB & Spring Data

Integration Testing is an often overlooked area in enterprise development. This is primarily due to the associated complexities in setting up the necessary infrastructure for an integration test. For applications backed by databases, it’s fairly complicated and time-consuming to setup databases for integration tests, and also to clean those up once test is complete (ex. data files, schemas etc.), to ensure repeatability of tests. While there have been many tools (ex. DBUnit) and mechanisms (ex. rollback after test) to assist in this, the inherent complexity and issues have been there always.

But if you are working with MongoDB, there’s a cool and easy way to do your unit tests, with almost the simplicity of writing a unit test with mocks. With ‘EmbedMongo’, we can easily setup an embedded MongoDB instance for testing, with in-built clean up support once tests are complete. In this article, we will walkthrough an example where EmbedMongo is used with JUnit for integration testing a Repository Implementation.

Here’s the technology stack that we will be using.

  • MongoDB 2.2.0
  • EmbedMongo 1.26
  • Spring Data – Mongo 1.0.3
  • Spring Framework 3.1

The Maven POM for the above setup looks like this.

Or if you prefer Gradle (by the way, Gradle is an awesome build tool which you should check out if you haven’t done so already).

To begin with, here’s the document that we will be storing in Mongo.

To assist with storing and managing this document, let’s write up a simple Repository implementation. The Repository Interface is as follows.

And the implementation…

To wire this up, we need a Spring Bean Configuration. Note that we do not need this for testing. But for the sake of completion, I have included this. The XML configuration is as follows.

And now we are ready to write the Integration Test for our Repository Implementation using Embed Mongo.

Ideally, the integration tests should be placed in a separate source directory, just like we place our unit tests (ex. src/test/java => src/integration-test/java). However, neither Maven nor Gradle supports this out of the box (yet – v1.2. For Gradle, there’s an on going discussion for this facility).

Nevertheless, both Maven and Gradle are flexible, so you can configure the POM / build.gradle to handle this. However, to keep this discussion simple and focused, I will be placing the Integration Tests in the ‘src/test/java’, but I do not recommend this for a real application.

Let’s start writing up the Integration Test. First, let’s begin with a simple JUnit based Test for the methods.

When this JUnit Test Case initializes, we need to fire up EmbedMongo to start an embedded Mongo server. Also, when the Test Case ends, we need to cleanup the DB. The below code snippet does this.

The initializeDB() method is annotated with @BeforeClass to start this before test case beings. This method fires up an embedded MongoDB instance which is bound to the given port, and exposes a Mongo object which is set to use the given database. Internally, EmbedMongo creates the necessary data files in temporary directories.

When this method executes for the first time, EmbedMongo will download the necessary Mongo implementation (denoted by Version.V2_2_0 in above code) if it does not exist already. This is a nice facility specially when it comes to Continuous Integration servers. You don’t have to manually setup Mongo in each of the CI servers. That’s one less external dependency for the tests.

In the shutdownDB() method, which is annotated with @AfterClass, we stop the EmbedMongo process. This triggers the necessary cleanups in EmbedMongo to remove the temporary data files, restoring the state to where it was before Test Case was executed.

We have now updated setUp() method to build a Spring MongoTemplate object which is backed by the Mongo instance exposed by EmbedMongo, and to setup our RepoImpl with that template. The tearDown() method is updated to drop the ‘Sample’ collection to ensure that each of our test methods start with a clean state.

Now it’s just a matter of writing the actual test methods.

Let’s start with the save method test.

We create a Sample object, pass it to, and assert to make sure that there’s only one Sample in the Sample collection. Simple, straight-forward stuff.

And here’s the test method for findByKey method.

Initially, we setup the data by adding a set of Sample objects into the data store. It’s important that we directly use here, because is a method under-test. We are not testing that here, so we use the underlying “trusted” during data setup. This is a basic concept in Unit / Integration testing. Then we execute the method under test ‘findByKey’, and assert to ensure that only two Samples matched our query.

Likewise, we can continue to write more tests for each of the repository methods, including negative tests. And here’s the final Integration Test file.

On a side note, one of the key concerns with Integration Tests is the execution time. We all want to keep our test execution times as low as possible, ideally a couple of seconds to make sure that we can run all the tests during CI, with minimal build and verification times. However, since Integration Tests rely on underlying infrastructure, usually Integration Tests take time to run. But with EmbedMongo, this is not the case. In my machine, above test suite runs in 1.8 seconds, and each test method takes only .166 seconds max. See the screenshot below.

MongoDB Integration Tests

I have uploaded the code for above project into GitHub. You can download / clone it from here: For more information regarding EmbedMongo, refer to their site at GitHub


  1. “[...] because is a method under-test. We are not testing that here, so we use the underlying “trusted” during data setup. This is a basic concept in Unit / Integration testing”

    I agree this is a valid thinking for unit tests, but not necessarily for integration tests.

    I understand your point but there are also other ways to look at this. One could use like this (pseudocode):
    public void testSave() { … }

    public void testFindByKey() {

    What it says is the following: “as long as saving does not work, there is no point in validating the retrieval”. And the second test won’t be executed if testSave fails.

    Dependencies between tests can be tricky but they have some valid uses once you move from unit to integration tests. JUnit does not allow them because it was built with UNIT tests in mind, but other frameworks do.

    1. Hi Tomek,

      Yes, I agree with you. If JUnit had a dependency facility like you suggested in your pesudeo code, that would have been awesome. If I recall correctly TestNG does support this type of dependency handling.

      Thanks for sharing your thoughts. Appreciate it a lot.


    1. Hello Sridhar,

      Thank you for your comment. Me, being dumb, wrote this article originally in my blog. Because I am a DZone MVB, DZone republishes my content with my permission on their site as well. If you spend some time to read the DZone page with some attention, you will see that it’s posted under my name, and at the bottom of it, they give credits to me and link back to this page as the source.

      Appreciate your feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>