Redis Stubbing
Commercial
The specmatic-redis module described in this document is available in the Pro plan or higher. Please get in touch with us through the Contact Us form at specmatic.io if you’d like to try it out.
Introduction to Redis Stubbing
Redis is an open source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue.
Pre-requisite Setup
The following dependency needs to be added to your application’s build.gradle or pom.xml
-
{ "sources": [ { "provider": "git", "repository": "<Git URL>", "consumes": [ "com/example/api_order_v1.yaml", "com/example/api_user_v1.yaml" ], "provides": [ "com/example/api_auth_v1.yaml", ] } ] } -
sources: - provider: git repository: <Git URL> consumes: - com/example/api_order_v1.yaml - com/example/api_user_v1.yaml provides: - com/example/api_auth_v1.yaml -
<dependency> <groupId>io.specmatic</groupId> <artifactId>specmatic-redis</artifactId> <scope>test</scope> <version>0.9.5</version> </dependency> -
testImplementation("io.specmatic:specmatic-redis:0.9.5")
Specmatic-Redis can be used not only in JVM environments but also via Docker images, making it possible to use it with other languages like Python through Redis clients.
For more information, please refer to specmatic-redis-python-sample
Managing Redis Server
The code below starts a Redis stub server:
RedisStub redisStub = new RedisStub();
redisStub.start();
To shut down the redis stub server:
redisStub.stop();
Setting Expectations
Setting expectation for a string response
redisStub.when("get")
.with(new String[]{"PO:NAME"})
.thenReturnString("John Wick");
Setting expectation for a JSON string response
redisStub.when("get")
.with(new String[]{"PO:NAME"})
.thenReturnJsonString("{\"name\": \"test\"}");
Setting expectation for a Long/Integer response
redisStub.when("decr")
.with(new String[]{"PO:ID"})
.thenReturnLong(1234567);
Setting expectation for an array response
redisStub.when("lrange")
.with(new String[]{"address", "1", "2"})
.thenReturnArray(new String[]{"22B Baker Street", "London"});
Sample Applications
Please have a look at one of the below-mentioned sample applications to understand how to utilize Specmatic-Redis in your application: