1, an overview of the

Spring Data Redis provides a simple way to integrate with Redis instances.

However, in some cases, it is more convenient to create a development and test environment using an embedded server than using a real server.

Therefore, we will learn how to set up and use an embedded Redis server.

2, rely on

Let’s first add the necessary dependencies:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
  <groupId>it.ozimov</groupId>
  <artifactId>embedded-redis</artifactId>
  <version>0.7.2</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
Copy the code

This spring-boot-starter-test contains various dependencies that we need to run integration tests.

In addition, embedded-Redis contains the embedded server we will use.

3, set up

After adding the dependencies, we should define the connection Settings between the Redis server and our application.

Let’s first create a class to hold our attributes:

@Configuration
public class RedisProperties {
    private int redisPort;
    private String redisHost;

    public RedisProperties(
      @Value("${spring.redis.port}") int redisPort, 
      @Value("${spring.redis.host}") String redisHost) {
        this.redisPort = redisPort;
        this.redisHost = redisHost;
    }

    // getters
}
Copy the code

Next, we should create a configuration class to define the connection and use our properties:

@Configuration
@EnableRedisRepositories
public class RedisConfiguration {

    @Bean
    public LettuceConnectionFactory redisConnectionFactory( RedisProperties redisProperties) {
        return new LettuceConnectionFactory(
          redisProperties.getRedisHost(), 
          redisProperties.getRedisPort());
    }

    @Bean
    publicRedisTemplate<? ,? > redisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate<byte[].byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        returntemplate; }}Copy the code

The configuration is very simple. So our embedded server can run on other ports.

4. Embedded Redis server

Now we will configure the embedded server and use it in one of our tests.

First, let’s create an application.properties file in the test’s resources directory (SRC /test/resources) :

spring.redis.host=localhost
spring.redis.port=6370
Copy the code

After that, we’ll create a configuration class annotated by @TestConfiguration:

@TestConfiguration
public class TestRedisConfiguration {

    private RedisServer redisServer;

    public TestRedisConfiguration(RedisProperties redisProperties) {
        this.redisServer = new RedisServer(redisProperties.getRedisPort());
    }

    @PostConstruct
    public void postConstruct(a) {
        redisServer.start();
    }

    @PreDestroy
    public void preDestroy(a) { redisServer.stop(); }}Copy the code

When the context context starts, the server starts with it. It runs on our machine according to the ports we define in the properties. With this, we can now run tests without stopping the actual Redis server.

Ideally, we’d like to start it on randomly available ports, but embedded Redis doesn’t have that yet. What we can do now is get random ports through the ServerSocket API.

In addition, when the context stops, the server stops with it.

The server can also be provided by our own executable:

this.redisServer = new RedisServer("/path/redis", redisProperties.getRedisPort());
Copy the code

In addition, executables can be defined for different operating systems:

RedisExecProvider customProvider = RedisExecProvider.defaultProvider()
.override(OS.UNIX, "/path/unix/redis")
.override(OS.Windows, Architecture.x86_64, "/path/windows/redis")
.override(OS.MAC_OS_X, Architecture.x86_64, "/path/macosx/redis");

this.redisServer = new RedisServer(customProvider, redisProperties.getRedisPort());
Copy the code

Finally, let’s create a test that uses the TestRedisConfiguration class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestRedisConfiguration.class)
public class UserRepositoryIntegrationTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void shouldSaveUser_toRedis(a) {
        UUID id = UUID.randomUUID();
        User user = new User(id, "name"); User saved = userRepository.save(user); assertNotNull(saved); }}Copy the code

This saves the user to our embedded Redis server.

In addition, we must manually add TestRedisConfiguration to SpringBootTest. As we said earlier, the server starts before the test and stops after the test.

5, conclusion

The embedded Redis server is the perfect tool to replace a real server in a test environment. We’ve seen how to configure it and how to use it in our tests.

More technical dry goods, please visit my personal website https://pinmost.com, or follow the public number [code farmer panda]