Skip to main content

Running integration tests and unit tests separately in Maven

Posted by carcassi on April 21, 2011 at 1:56 PM PDT

If you, like me, need to build libraries that need to be tested against a live server (integration tests), here's what you can do.

Have all your integration tests either in seprate "itest" packages, such as my.library.itest, or have the test classes end in ITest, such as my.library.ConnectITest. In the main section, have the surefire plugin exclued running the integration tests:

    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/*$*</exclude>
                        <exclude>**/*ITest.java</exclude>
                        <exclude>**/itest/*.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
            ...
        </plugins>
     </build>

 This way, by default, a freshly checked out copy of your project will build successfully (regardless of whether the infrastructure for the tests is present). Then add an "ITest" profile that configures surefire to remove the exclude:

    <profiles>
        <profile>
            <id>ITest</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <argLine>-Dmy.location=http://localhost:8080/MyService</argLine>
                            <excludes>
                                <exclude>**/*$*</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

You can also add an property to specify the location of your test service (so that you are not hardcoding the location, and if you accidentally include an integration test with the unit tests, you see it failing during a normal build because it cannot find the location). When you are done, if you run "maven build -PITest" you will run all tests (including the integration tests) and if you run "maven build" you'll only run the unit.

Within NetBeans, you can right click on your Maven projection, "Set Configuration" and select ITest, at which point you will always be running all tests. Or you can go back and select the default, and run only the unit.

Comments

<p>Hi,</p> <p>you should consider the maven failsafe plugin ...

Hi,
you should consider the maven failsafe plugin which is designed for seprating integration tests from unit tests. I am using it in all maven projects and until now it works like a charm. See maven.apache.org/plugins/maven-failsafe-plugin/.
regards Marcel

Hi Marcel, I defintely will look into it! I kept looking ...

Hi Marcel,

I defintely will look into it! I kept looking for something like this, and I never found anything...

Thanks!