Class WithEmbeddedRedis

java.lang.Object
fr.ght1pc9kc.testy.redis.WithEmbeddedRedis
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver

public class WithEmbeddedRedis extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.ParameterResolver
Allow to launch en Embedded Redis DB

Use embedded-redis from codemonstur which seem to be the most maintained fork of the original kstyrc version

From this database, an async RedisServer is created and shared in the context.

This extension allow to injection of :

  • @RedisPort int redisPort: The redis listening port
  • RedisClient client: The redis lettuce client connected to the embedded server
  • RedisServer server: The redis server itself

Usage :

With @ExtendWith


  @ExtendWith(WithEmbeddedRedis.class)
  class WithEmbeddedRedisTest {
      @Test
      void should_use_embedded_redis(RedisClient client) {
         try (StatefulRedisConnection<String, String> conn = client.connect()) {
             RedisCommands<String, String> redisCommands = conn.sync();
             String actual = redisCommands.set("key", "Hello, Redis!");
         }
     }
  }
 

With @RegisterExtension


 @RegisterExtension
 public static WithEmbeddedRedis mockRedis = WithEmbeddedRedis.builder().build();

 @Test
 void should_use_embedded_redis(RedisClient client) {
     try (StatefulRedisConnection<String, String> conn = client.connect()) {
         RedisCommands<String, String> redisCommands = conn.sync();
         String actual = redisCommands.set("key", "Hello, Redis!");
     }
 }
 
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    boolean
    supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WithEmbeddedRedis

      public WithEmbeddedRedis()
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
      Throws:
      Exception
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
      Throws:
      Exception
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException