I run my Integration Test cases with Spring boot with the help of my local redis-server on my machine.
But I want an embedded Redis server which is not dependent on any server and can run on any environment, like the H2 in-memory database. How can I do it?
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@SpringApplicationConfiguration(classes = Application.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MasterIntegrationTest{
}
You can use an embedded Redis like https://github.com/kstyrc/embedded-redis
Adjust the properties for your integration test to point to your embedded redis, for example :
Instanciate the embedded redis server in a component that is defined in your tests only :
you can see this repository: https://github.com/caryyu/spring-embedded-redis-server , fully integrated with Spring and Spring Boot
maven dependency
spring boot annotation
usage of application.yml
Another neat way is to use the
testcontainers
library which can run any type of application that can in a Docker container and Redis is no exception. What I like best is that it is lightly coupled with the Spring Test ecosystem.maven's dependency:
simple integration test:
You can use ozimov/embedded-redis as a Maven(-test)-dependency (this is the successor of kstyrc/embedded-redis).
Add the dependency to your pom.xml
Adjust your application properties for your integration test
Use the embedded redis server in a test configuration