January 21, 2016 / by Markus Heiden / Software engineer / @
Maven integration testing with Redis
We have experienced too much downsides of the redis-maven-plugin, which uses a Java reimplementation of Redis. So we decided to use a real Redis server for our integration tests.
Downsides of redis-maven-plugin
- Not all Redis commands have been fully implemented.
- Throws exception (which can be ignored) when used with Java 8.
- The Java reimplementation of Redis seems not to be maintained.
Solution
First you need to install a Redis on your local system. Make sure it is visible in your path. All you need to use it for integration testing with Maven is the exec-maven-plugin. The downside of using the exec-maven-plugin is, that the configuration is a bit lengthy.
pom.xml Snippet
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>launch-redis</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>redis-server</executable>
<arguments>
<argument>${project.basedir}/src/test/redis/redis.conf</argument>
<argument>--port</argument>
<argument>${redisPort}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>shutdown-redis</id>
<phase>post-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>redis-cli</executable>
<arguments>
<argument>-p</argument>
<argument>${redisPort}</argument>
<argument>shutdown</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
src/test/redis/redis.conf
# Redis local test configuration
daemonize yes
pidfile ./target/redis.pid
loglevel notice
logfile ./target/redis.log
bind 127.0.0.1
timeout 0
databases 1
loglevel notice
maxmemory 256mb
maxmemory-policy volatile-ttl
You may adapt some of these settings (e.g. number of databases and memory) for your purpose.