Java: Introducing Redis Mock
Take a look at a mock for a commonly used service (Redis) and how you can make use of this updated mock to help make your testing easier.
Join the DZone community and get the full member experience.Join For Free
Many software solutions built today often depend on external processes. Examples include: databases, web services, indexing services, etc.
This introduces challenges when testing software solutions. How can you simply test a solution in isolation without having to spin up instances of external processes that solution may depend on?
A common solution is to mock out the dependency. Mocking simply refers to the act of mimicking the expected results returned by the dependency, without having to mimic the behavior.
At Grakn, we found ourselves in a similar position. After introducing and making extensive use of redisq, we needed to either mock out Redis or have it running as a process in the background.
Through this great library, we were able to create an instance of Redis in order to test against. However, creating a running instance of Redis (or any external process) when wanting to test simple functionality becomes tedious and slows down your tests.
An alternative to this approach is using Mockito, which lets you to explicitly state what calls should return what answers. This is a very powerful approach to mocking out services but often requires a lot of fine-tuned and difficult to maintain mocking code customized to each test case.
Enter Redis Mock
We found an existing in-memory Redis mock providing some basic functionality. However, it has not been maintained since 2016 and lacks more of the advanced features of Redis. The original author has provided a great framework to build from (thank you zxl0714!). We have since forked this library and created our own version of an in-memory Redis mock.
We have extended this mock to support more complex Redis operations, as well as transactions and the publisher/subscriber model. Using this mock, we are able to eliminate the need for creating a running instance of Redis.
We would now like to share this updated version of Redis Mock which can be downloaded and used by anyone.
How Does It Work?
The mock functions by creating a server which accepts web socket connections. Each Redis connection made to the mock creates a new RedisClient instance running on its own thread. The socket connections mimic the Redis protocol.
For example, a valid stream input such as...
*3 $3 SET $5 mykey $5 Hello
.... gets parsed into a Java object representing the
set operation. This
set operation synchronizes against the in-memory
RedisBase and executes when it is able to. In this way, we mimic the single threaded nature of Redis despite being a multi-threaded mock.
We chose to make a multi-threaded mock in order to more easily support transactions and the publisher/subscriber model. In the future, we may try to simplify it.
How to Use It
To use the mock, you must first add a reference to it in your
<dependency> <groupId>ai.grakn</groupId> <artifactId>redis-mock</artifactId> <version>0.1.3</version> </dependency>
After that, you can start and stop your mock Redis server with:
RedisServer server = RedisServer.newRedisServer(port); server.start(); server.stop();
If a port is not provided, a random one is used. From this point on, you can connect to the mock as you would with any normal running instance of Redis:
Jedis jedis = new Jedis(server.getHost(), server.getBindPort); ... jedis.close()
When to Use It
This mock is ideal when wanting to simplify your testing stack. It makes running tests easier and lighter due to not requiring any external processes. It is entirely contained within the JVM that created it.
When Not to Use It
This mock should not be used when performing scale testing. The mock is thread safe but is not as memory efficient as a real running instance of Redis.
By replacing Embedded Redis with this mock, we were able to speed up our tests by more than 20%. This has enabled us to get faster feedback on running test. However, we still use Embedded Redis when running scale tests.
We are looking to continually maintain and improve this library. We appreciate and look forward to any feedback.
Please get in touch if you’ve any questions or comments, either below, via our Community Slack channel, or via our discussion forum.
Published at DZone with permission of Filipe Peliz Pinto Teixeira, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
You’ve Got Mail… and It’s a SPAM!
Mainframe Development for the "No Mainframe" Generation
Application Architecture Design Principles
Explainable AI: Making the Black Box Transparent