Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding integration tests in Eclipse IDE with JUnit 4

I have two kinds of tests in my Java (Maven) web project: "normal" unit-tests and integration tests using an embedded Tomcat 7 server and Selenium for automated GUI testing on Jenkins. All tests are annotated with JUnit's @Test, normal tests end with "Test.java" while integration tests end with "IntegrationTest.java". All test-classes are located in src/test/java

I normally build my project with mvn clean verify, while the relevant part of my pom.xml which starts the tomcat server and splits the test-categories accordingly looks like this:

    <!-- For front-end testing -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <uriEncoding>UTF-8</uriEncoding>
                <additionalConfigFilesDir>${basedir}/conf</additionalConfigFilesDir>
                <contextFile>${basedir}/src/test/resources/context.xml</contextFile>
            </configuration>
            <executions>
                <execution>
                    <id>start-tomcat</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run-war-only</goal>
                    </goals>
                    <configuration>
                        <fork>true</fork>
                        <port>9090</port>
                    </configuration>

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

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <excludes>
                    <exclude>**/*IntegrationTest*</exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <includes>
                    <include>**/*IntegrationTest*</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

This procedure works fine, except when I want to run my tests in eclipse where I normally right-click on my project -> run as -> JUnit Tests. By selecting this option all my tests (including the integration tests) are run. The integration-tests fail in this case because Tomcat is not running (it is only started in Maven's pre-integration-test phase).

How can I exclude these tests in Eclipse with JUnit plugin?

like image 425
Raphael Roth Avatar asked Dec 23 '15 15:12

Raphael Roth


1 Answers

I use junit-toolbox for that. It provides annotations to separate unit and integration tests via wildcard patterns.

<dependency>
    <groupId>com.googlecode.junit-toolbox</groupId>
    <artifactId>junit-toolbox</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

The base package under e.g. /src/test/java/base contains two classes –

AllUnitTests.java:

package base;

import org.junit.runner.RunWith;

import com.googlecode.junittoolbox.ParallelSuite;
import com.googlecode.junittoolbox.SuiteClasses;

/**
 * This detects all (fast running) unit test classes by the given naming pattern.
 * 
 */
@RunWith(ParallelSuite.class)
@SuiteClasses({ "**/*Test.class", "!**/*IntegrationTest.class", "!**/*LearningTest.class" })
public class AllUnitTests {

}

and AllIntegrationTests.java:

package base;

import org.junit.runner.RunWith;

import com.googlecode.junittoolbox.SuiteClasses;
import com.googlecode.junittoolbox.WildcardPatternSuite;

/**
 * This detects all integration test classes by the given naming pattern.
 * 
 */
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({ "**/*IntegrationTest.class", "**/*IT.class" })
public class AllIntegrationTests {

}

You can run both generic test suites via Eclipse.

To be able to also run the tests via Maven I use an approach which is similar to the one you have shown.

like image 188
Jens Piegsa Avatar answered Nov 12 '22 08:11

Jens Piegsa