Bragolgirith
Bragolgirith

Reputation: 2238

JUnit - run tests in a category only if specific profile is active

I have the following tests:

FirstUnitTest.java
SecondUnitTest.java
FirstIntegrationTest.java
SecondIntegrationTest.java

The unit tests are not marked with a category.

The two integration tests are marked with @Category(IntegrationTests.class).

I want by default to run all tests EXCEPT for the integration tests. If, however, a profile integration-tests-only is active, i want to run ONLY the integration tests.

I naively thought the following configuration would make this work:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludedGroups>com.example.IntegrationTests</excludedGroups>
            </configuration>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>integration-tests-only</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <groups>com.example.IntegrationTests</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

But while running the tests without a profile does exactly what I want - run only the unit tests, if I activate the integration-tests-only profile no tests run at all.

Any ideas what I'm doing wrong?

Upvotes: 4

Views: 2320

Answers (1)

shlomi33
shlomi33

Reputation: 1568

I assume that this happens because you include and exclude, and Maven merges the configurations and resolves to run nothing.

Consider this re-write of the config (did not run it so might have some minor issues):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <executions>
                <execution>
                    <id>default-test</id>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </execution>
                <execution>
                    <id>unit-tests</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skip>${skipUnitTests}</skip>
                        <excludedGroups>com.example.IntegrationTests</excludedGroups>
                    </configuration>
                </execution>
                <execution>
                    <id>integ-tests</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skip>${skipIntegTests}</skip>
                        <groups>com.example.IntegrationTests</groups>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>no-tests</id>
        <properties>
            <skipTests>true</skipTests>
        </properties>
    </profile>
    <profile>
        <id>unit-tests</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <skipUnitTests>false</skipUnitTests>
            <skipIntegTests>true</skipIntegTests>
        </properties>
    </profile>
    <profile>
        <id>integ-tests</id>
        <properties>
            <skipUnitTests>true</skipUnitTests>
            <skipIntegTests>false</skipIntegTests>
        </properties>
    </profile>
</profiles>

Upvotes: 5

Related Questions