skip to Main Content

I am using embedded Redis in our Webflux testcases using the following dependency in pom.xml file

<dependency>
    <groupId>com.github.kstyrc</groupId>
    <artifactId>embedded-redis</artifactId>
    <version>0.6</version>
</dependency>

And using below annotations for controllertest classes

@RunWith(SpringRunner.class)
@AutoConfigureWebTestClient(timeout = "36000000")
@WithMockUser
@TestPropertySource(locations = "classpath:application-test.properties")
@SpringBootTest(classes = Application.class)
@DirtiesContext

When I run individual controllertest class, all the controllertest are passing.

When I start both the controllertest classes, one controllertest class is failing with following error:

Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embededRedis': Invocation of init method failed; nested exception is java.lang.RuntimeException: Can't start redis server. Check logs for details.

Below is the embedded Redis class I am using in my JUnit test

@Component
public class EmbededRedis {
    @Value("${spring.redis.port}")
    private int redisPort;
    private RedisServer redisServer;

    @PostConstruct
    public void startRedis() throws IOException {
        if(redisServer==null||!redisServer.isActive()) {
        redisServer = new RedisServer(redisPort);
        redisServer.start();
    }
    }

    @PreDestroy
    public void stopRedis() {
        if(redisServer!=null) {
        redisServer.stop();
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    This issue got resolved after restarting the system


  2. Check if Redis port is available or not. if not, try to assign a different port

    @Component
    public class EmbededRedis {
        @Value("${spring.redis.port:9999}")
        private int redisPort;
        private RedisServer redisServer;
    
        @PostConstruct
        public void startRedis() throws IOException {
            if(redisServer==null||!redisServer.isActive()) {
            redisServer = new RedisServer(redisPort);
            redisServer.start();
        }
        }
    
        @PreDestroy
        public void stopRedis() {
            if(redisServer!=null) {
            redisServer.stop();
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search