How can we use maven-redis-plugIn while writing Integration Test cases in Spock

406 Views Asked by At

I am writing Integration tests using spock-framework, I am able to use maven-cassandra-plugin i.e before executing Integration tests cassandra gets started and loads the cql file(which we mentioned in the configuration attribute of maven-cassandra-plugin then our tests will run, after running the tests cassandra will get shutdown. In the same way I wanted to use maven-redis-plugin. I have seen following plugin samples in web

<plugin>
            <groupId>ru.trylogic.maven.plugins</groupId>
            <artifactId>redis-maven-plugin</artifactId>
            <version>1.4.6</version>
            <configuration>
                <forked>true</forked>
            </configuration>
            <executions>
                <execution>
                    <id>launch-redis</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>

                </execution>
                <execution>
                    <id>stop-redis</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>shutdown</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Sample 2:

<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>

I want to insert some data into cache, then using my Integration test classes I want to perform CRUD operations on that data

1

There are 1 best solutions below

1
On

By the time Spock starts to execute, the Redis is already supposed to be up and running.

So, it makes sense to populate some "test" data right before the test. In Spock you can use one of the following (its called 'fixture' methods):

def setup() {}          // run before every feature method
def cleanup() {}        // run after every feature method
def setupSpec() {}     // run before the first feature method
def cleanupSpec() {}   // run after the last feature method

See Here more information about these fixture methods

Even if the test fails, the cleanup / cleanupSpec methods will be called by the framework, which is a good hook to cleanup the redis data.

Of course, in order to make all this work, you'll have to set up the Redis connection.

Since its an integration test, the chances are that the redis driver layer will start from the test in any case, so you'll be able to reuse it if you're running it with Spring or something. Its hard to tell more details given the information you've provided, but in any case the data should be populated right before the test and cleaned up right after the test.