Search code examples
springspring-mvcjpaseleniummaven-tomcat-plugin

Using "test" DB in GUI integration tests


I have a Spring MVC + JPA/Hibernate application. The application has some GUI (Selenium) tests. I use tomcat7-maven-plugin to deploy my application for these tests:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <fork>true</fork>
            </configuration>
        </execution>
        <execution>
            <id>tomcat-shutdown</id>
            <goals>
                <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
        </execution>
    </executions>
</plugin>

This setup works fine with the production DB, but I need to use another DB for these tests. My DB connection settings are defined in a Spring config and .properties files.

How can I change the DB connection settings when I deploy the application for GUI tests?


Solution

  • 1) Create two Spring profiles in the Spring config: one for production (containing DB connection settings for the production DB), and another for integration tests (containing DB connection settings for the test DB).

    2) Add the following configuration to tomcat7-maven-plugin:

    <configuration>
        <systemProperties>
            <!--Replace `nameOfYourSpringTestProfile` with the name of your actual test profile. -->
            <spring.profiles.active>nameOfYourSpringTestProfile</spring.profiles.active>
        </systemProperties>
    </configuration>
    

    This configuration will set the spring.profiles.active system variable in the deployed application, which will activate the test profile and the corresponding DB connection settings.