Git Product home page Git Product logo

mock-jedis's Introduction

Mock Jedis

Mock Jedis is a library for mocking out Jedis clients. It's useful for testing your code without actually having a live redis server up. Currently, mock-jedis supports pipelining and all the basic Jedis commands, but if you find missing functionality you're welcome to submit a pull request.

Compile

with gradle (preferred method):

gradle build

Adding mock-jedis to your project

Add it as a dependency to your project.

Here's a sample gradle script that will pull mock-jedis 0.4.0 from maven-central

buildscript {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'

dependencies {
  testCompile 'com.fiftyonred:mock-jedis:0.4.0'
}

Sample maven dependency definition:

<dependency>
    <groupId>com.fiftyonred</groupId>
    <artifactId>mock-jedis</artifactId>
    <version>0.4.0</version>
    <type>jar</type>
    <scope>test</scope>
</dependency>

Using mock-jedis

Jedis j = new MockJedis("test");
j.set("test", "123");
assertEquals("123", j.get("test"));

Supported Commands

Currently the following commands are supported by mock-jedis

  • KEYS: DEL DUMP EXISTS EXPIRE EXPIREAT KEYS PERSIST PEXPIRE PEXPIREAT PTTL RANDOMKEY RENAME RENAMENX RESTORE TTL TYPE
  • STRINGS: APPEND DECR DECRBY GET GETSET INCR INCRBY INCRBYFLOAT MGET MSET MSETNX PSETEX SET SETEX SETNX STRLEN
  • HASHES: HDEL HEXISTS HGET HGETALL HINCRBY HINCRBYFLOAT HKEYS HLEN HMGET HMSET HSET HSETNX HVALS
  • LISTS: LLEN LPOP LPUSH
  • SETS: SADD SCARD SDIFF SDIFFSTORE SINTER SINTERSTORE SISMEMBER SMEMBERS SMOVE SPOP SRANDMEMBER SREM SUNION SUNIONSTORE
  • CONNECTIONS: ECHO PING SELECT QUIT
  • SERVER: DBSIZE FLUSHALL FLUSHDB
  • PIPELINES

Unsupported Things

  • All commands not listed above

mock-jedis's People

Contributors

bittrance avatar bsideup avatar fniwes avatar idyedov avatar jitpack-io avatar kshchepanovskyi avatar volker48 avatar vpranckaitis avatar zzgab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mock-jedis's Issues

can you support rpush?

Getting a connection refused error currently when I try to call rpush because it is using the inherited method, which is trying to connect to the real thing. In general, it would be nice to override every method and make it do nothing as a default.

I'll fork and send a push request for this.

Expiration support

Hi!

First, thank you for this project, really good idea.

Sadly it's not suitable for us because of expriration support.

Why it's not implemented? I mean, it's so because of some technical troubles or it's just not implemented yet?

Thanks!

lpush actually does an rpush

A test such as the following one fails. When retrieving the elements with lrange, they come out in reverse order.

    @Test
    public void lpushShouldPushToTheFrontOfAList() {

        // given
        MockJedis jedis = new MockJedis("localhost");
        jedis.lpush("list", "c");
        jedis.lpush("list", "b");
        jedis.lpush("list", "a");

        // when
        List<String> list = jedis.lrange("list", 0, -1);

        // then
        assertThat(list, is(asList("a", "b", "c")));
    }

I guess the root of the bug is in com.fiftyonred.mock_jedis.MockStorage#lpush where values are added via Collections.addAll.

Redundant host argument in MockJedis

Why MockJedis constructor has host argument?
You can set Jedis host argument to default value, "test" for example and add MockJedis() default constructor.

Is mock-jedis thread safe?

I'm currently using a real Redis database in my unit tests, and I'm curious if I could used mock-jedis instead.

My unit tests run multiple threads against the real Redis database--if I used mock-jedis instead, would that work?

NPE when renaming a set

When I rename a set and then try to access the new set, I get a NullPointerException. Here's a reproduction of the problem using Groovy shell, but I get the same problem using plain-old Java 1.7.

groovy:000> m.sadd('s', 'alpha'); ===> 1 groovy:000> m.sadd('s', 'beta'); ===> 1 groovy:000> m.smembers('s'); ===> [beta, alpha] groovy:000> m.rename('s','s2'); ===> OK groovy:000> m.smembers('s'); ===> [] groovy:000> m.smembers('s2'); ERROR java.lang.NullPointerException: null at com.fiftyonred.mock_jedis.MockPipeline.smembers (MockPipeline.java:1516) at com.fiftyonred.mock_jedis.MockJedis.smembers (MockJedis.java:655) at redis.clients.jedis.JedisCommands$smembers.call (Unknown Source) at groovysh_evaluate.run (groovysh_evaluate:3) ...

Feature request: Mock Server failures

An interesting test case would be test failure behavior.
This could be a flag passed into the constructor which throws JedisException for all API calls like get(), set(). A bit of more enhancement on top of this feature would be to have the possibility of failure on a random percentage.
This feature would make mock-jedis attain perfection ๐Ÿ‘

hget refering to an non-existing field throws NullPointerException

in MockPipeline#hget(String key, String field)
you should account for the fact that hashStorage.get(key).get(field) can be null.
This is the case if the hash is defined but has no field with name field.

Fix:

if (hashStorage.containsKey(key)) {
Map<String, String> hash = hashStorage.get(key);
if (hash.containsKey(field)) {
response.set(hash.get(field).getBytes());
} else {
response.set(null); //important, otherwise we'll get an exception when retrieving the value
}
}

MockJedisPool not working for scala

Exception:
redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
[info] at redis.clients.util.Pool.returnResourceObject(Pool.java:65)
[info] at org.sedis.Pool.withJedisClient(sedis.scala:105)
[info] at com.remarkmedia.dragon.service.infrastructure.oauth2.OauthClient$.validate(OauthClient.scala:39)
[info] at com.remarkmedia.dragon.service.infrastructure.oauth2.OauthClientSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(OauthClientSpec.scala:23)
[info] at com.remarkmedia.dragon.service.infrastructure.oauth2.OauthClientSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(OauthClientSpec.scala:23)
[info] at com.remarkmedia.dragon.service.infrastructure.oauth2.OauthClientSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(OauthClientSpec.scala:23)

Code:
val jedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost")
implicit val sedisPool = new Pool(jedisPool)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.