Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


1. Overview

Spring Data Redis provides an easy way to integrate with Redis instances.

However, in some cases, it's more convenient to use an embedded server than to create an environment with a real server.

Therefore, we'll learn how to set up and use the Embedded Redis Server.

2. Dependencies

Let's begin by adding the necessary dependencies:




The spring-boot-starter-test dependency contains everything we need to run integration tests.

Additionally, the embedded-redis contains the embedded server that we'll use.

3. Setup

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

Let's begin by creating a class that will hold our properties:

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

Next, we should create a configuration class that defines the connection and uses our properties:

public class RedisConfiguration {

    public LettuceConnectionFactory redisConnectionFactory(
      RedisProperties redisProperties) {
        return new LettuceConnectionFactory(

    public RedisTemplate<?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
        return template;

The configuration is quite simple. Additionally, it allows us to run the embedded server on a different port.

Check out our Introduction to Spring Data Redis article to learn more about the Redis with Spring Boot.

4. Embedded Redis Server

Now, we'll configure the embedded server and use it in one of our tests.

Firstly, let's create an application.properties file in the test resource directory (src/test/resources):


After that, we'll create a @TestConfiguration-annotated class:

public class TestRedisConfiguration {

    private RedisServer redisServer;

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

    public void postConstruct() {

    public void preDestroy() {

The server will start once the context is up. It'll start on our machine on the port that we've defined in our properties. For instance, we can now run the test without stopping the actual Redis server.

Ideally, we'd like to start it on the random available port but embedded Redis doesn't have this feature yet. What we could do right now is to get the random port via the ServerSocket API.

Additionally, the server will stop once the context is destroyed.

The server can also be provided with our own executable:

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

Furthermore, the executable can be defined per operating system:

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());

Finally, let's create a test that'll use our TestRedisConfiguration class:

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

    private UserRepository userRepository;

    public void shouldSaveUser_toRedis() {
        UUID id = UUID.randomUUID();
        User user = new User(id, "name");

        User saved = userRepository.save(user);


The user has been saved to our embedded Redis server.

Additionally, we had to manually add TestRedisConfiguration to SpringBootTest. As we said earlier, the server has started before the test and stopped after.

5. Conclusion

The Embedded Redis Server is the perfect tool to replace the actual server in the test environment. We've seen how to configure it and how to use it in our test.

As always, the code for examples is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


Junit footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!