Is your test suite taking a large amount of time to run just because your methods need some database queries to be handled? Testing with In-memory databases can save a hell lot of time. In-memory database makes the queries readily available to methods in a matter of milliseconds. In case you are using mongoDB, this blog can help you run your test suite much faster than currently it is.
I assume you are already interacting with mongoDB in your application and have included mongoDB driver for that. This plug-in for in-memory mongoDB works with scalatest. I also assume you are using Scalatest as test Toolkit. If you are not, please include the following dependencies to your build to include embedded mongoDB and scalatest.
Once you have added these lines into your build, resolve these dependencies by running ‘sbt clean compile’ before you proceed.
Preparing the Test Suite
I hope you have written a Test Class to test your code. If you’ve not created any, create one. Your test class must have the following imports:
These imports are necessary to start an in-memory instance of mongoDB before running your Test Suite. Extend your test class with FunSuite (ignore if import is already there) and mix with the traits BeforeAndAfter and MongoEmbedDatabase. For example, have a look at the following code snippet.
Here InsertPersonTest is my test class and I’ve extended it with FunSuite and mixed the traits BeforeAndAfter and MongoEmbedDatabase.
Starting Embedded Mongo
Now the goal is to start the mongoDB instance before running the Test Suite and Stopping it afterwards. To hold the reference of In-memory mongoDB instance, let us declare a variable in the test class as mongoInstance and then include the following before and after statements in your test class. Follow the given code snippet:
This will start an in-memory instance of mongoDB on the given port, i.e. (27017: default port of mongoDB). Please ensure that no local instance of mongoDB server is running on your machine before running your test suite. Stop the mongoDB server if it is running already and then run your test cases. I hope this will accomplish your purpose of in-memory mongoDB in testing environment.
You can clone the example code from the EmbededMongoExample repository on the github. This project illustrates the working of in-memory mongoDB instances with example test cases.