Last active
August 29, 2015 13:57
-
-
Save skazhy/9847529 to your computer and use it in GitHub Desktop.
mocking scala-redis RedisClientPool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "foo" | |
version := "1.0" | |
libraryDependencies ++= Seq( | |
"net.debasishg" % "redisclient_2.10" % "2.12", | |
"org.specs2" %% "specs2" % "2.3.10" % "test", | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.specs2.mutable._ | |
import org.specs2.mock._ | |
object Spec extends Specification with Mockito { | |
"Fetching existing key" should { | |
"return it's value" in { | |
val c = mock[com.redis.RedisClientPool] | |
val wrap = new RedisWrapper(c) | |
c.withClient(anyFunction1[com.redis.RedisClient, Option[String]]) returns Some("value") | |
wrap.getOrElse("key", "fallback") mustEqual "value" | |
} | |
} | |
"Fetching absent key" should { | |
"return fallback value" in { | |
val c = mock[com.redis.RedisClientPool] | |
val wrap = new RedisWrapper(c) | |
c.withClient(anyFunction1[com.redis.RedisClient, Option[String]]) returns None | |
wrap.getOrElse("key2", "fallback") mustEqual "fallback" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.redis._ | |
class RedisWrapper(c: RedisClientPool) { | |
def set(k: String, v: String) = c.withClient(_.set(k, v)) | |
/** A method to demonstrate that value is returned from Redis & transformed. */ | |
def getOrElse(k: String, f: String) = c.withClient(_.get(k)).getOrElse(f) | |
} | |
object Foo { | |
def main(args: Array[String]) { | |
val e = new RedisWrapper(new RedisClientPool("localhost", 6379)) | |
e.set("foo", "bar") | |
printf(e.getOrElse("foo", "404")) // prints foo | |
printf(e.getOrElse("bar", "404")) // prints 404 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment