Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot run multiple executions in maven surefire?

I want to run test classes whose name end with ResourceTest.java, so I defined following execution.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/*.java</exclude>
        </excludes>
    </configuration>
    <version>2.12.2</version>
    <executions>
        <execution>
            <id>resource-tests</id>
            <phase>resource-tests</phase>
            <goals>
                <goal>resource-tests</goal>
            </goals>
            <configuration>
                <includes>**/*ResourceTest.java</includes>
                <!-- <exludes>**/*.java</exludes> -->
            </configuration>
        </execution>
    </executions>
</plugin>

But I'm not sure how to run this, I've searched a lot and I'm missing something.

I tried surefire:test, it skipped all the test cases as defined in above configuration. So, I tried surefire:resource-tests, maven is saying no goal is not defined.

I'm using eclipse to run my maven build, by passing these parameters. How can I run by the execution id?

How to select a specific execution when running with surefire:test when I've mulltiple executions defined in my pom?

What am I missing? Any help would be appreciated.

like image 868
pinkpanther Avatar asked Feb 09 '23 22:02

pinkpanther


1 Answers

There are several problems with your current configuration :

  • you are forcing the maven-surefire-plugin to be executed in the resource-tests phase but this phase does not exist. You should delete that declaration to keep the default plugin binding phase, which is test.
  • you are invoking the goal resource-tests but maven-surefire-plugin does not define such a goal.
  • the <includes> element is ill-defined. There should be a <include> tag under it.
  • you are excluding all Java files from the plugin configuration so no test will be run
  • the configuration of the plugin should be done under the <configuration> element not for each <executions>.
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.2</version>
    <configuration>
        <includes>
            <include>**/*ResourceTest.java</include>
        </includes>
    </configuration>
</plugin>

When you have multiple executions and you want "select" one of them, you can use a profile:

<profiles>
    <profile>
        <id>resource-tests</id>
        <properties>
            <test-classes>**/*ResourceTest.java</test-classes>
        </properties>
    </profile>
    <profile>
        <id>task-tests</id>
        <properties>
            <test-classes>**/*TaskTest.java</test-classes>
        </properties>
    </profile>
</profiles>

with the following plugin configuration:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.2</version>
    <configuration>
        <includes>
            <include>${test-classes}</include>
        </includes>
    </configuration>
</plugin>

With such a configuration:

  • when you run mvn clean test -Presource-tests, only the classes matching **/*ResourceTest.java will be tested
  • when you run mvn clean test -Ptask-tests, only the classes matching **/*TaskTest.java will be tested
like image 163
Tunaki Avatar answered Feb 11 '23 15:02

Tunaki