DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • 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

Trending

  • 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
  1. DZone
  2. Coding
  3. Java
  4. Java: Introducing Redis Mock

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.

Filipe Peliz Pinto Teixeira user avatar by
Filipe Peliz Pinto Teixeira
·
Updated Oct. 16, 17 · Tutorial
Like (7)
Save
Tweet
Share
47.46K Views

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 pom.xml with:

<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.

Conclusion

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.

Redis (company) Java (programming language) Testing

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.

Trending

  • 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

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: