oyindonesia/redis-mock


A simple redis java mock for unit testing

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.oyindonesia:redis-mock:0.1.6.1'
	}
	dependencies {
		implementation("com.github.oyindonesia:redis-mock:0.1.6.1")
	}
	<dependency>
	    <groupId>com.github.oyindonesia</groupId>
	    <artifactId>redis-mock</artifactId>
	    <version>0.1.6.1</version>
	</dependency>

                            
    libraryDependencies += "com.github.oyindonesia" % "redis-mock" % "0.1.6.1"
        
        

                            
    :dependencies [[com.github.oyindonesia/redis-mock "0.1.6.1"]]
        
        

Readme


GitHub release Build Status Slack Status

Redis-Mock

Redis Mock is a simple in-memory mock of redis for java testing. It allows you to test any behaviour dependent on redis without having to deploy an instance of redis

Quickstart

Add it as a dependency in Maven as:

<dependency>
  <groupId>ai.grakn</groupId>
  <artifactId>redis-mock</artifactId>
  <version>0.1.6</version>
</dependency>

Or use it to gradle dependency from oyindonesia

compile 'com.github.oyindonesia:redis-mock:master-SNAPSHOT'

add build.gradle repository

maven { url 'https://jitpack.io' }

Create a redis server and bind it to jedis:

private static RedisServer server = null;

@Before
public void before() throws IOException {
  server = RedisServer.newRedisServer();  // bind to a random port
  server.start();
}

@Test
public void test() {
  ...
  Jedis jedis = new Jedis(server.getHost(), server.getBindPort());
  ...
}

@After
public void after() {
  server.stop();
  server = null;
}

From here test as needed

Master and Slave

RedisServer master = newRedisServer();
RedisServer slave = newRedisServer();
master.setSlave(slave);

Fault tolerance testing

We can make a RedisServer close connection after every serveral commands. This will cause a connection exception for clients.

RedisServer server = RedisServer.newRedisServer();
ServiceOptions options = new ServiceOptions();
options.setCloseSocketAfterSeveralCommands(3);
server.setOptions(options);
server.start();