Cannot run multiple executions in maven surefire?
Asked Answered
M

1

5

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.

Mesdemoiselles answered 1/10, 2015 at 10:9 Comment(0)
Z
11

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
Zebulen answered 1/10, 2015 at 10:21 Comment(13)
now I can run this with surefire:test right? when I run like this, I'm seeing String cannot be cast to util.List error for <includes>**/*ResourceTest.java</includes> property. And also, when I specify multiple executions, how can select specfic execution?Mesdemoiselles
@Mesdemoiselles You can simply run mvn clean test. Since this plugin is bound by default to the test phase, it will be executed.Zebulen
@Mesdemoiselles Yes, I saw this afterwards and edited my post.Zebulen
thanks, I also could you tell me how to select a specific execution when I've multiple executions defined? for example, with id=task-tests?Mesdemoiselles
I updated my question, I'm trying to define multiple executions, hence I asked the question.Mesdemoiselles
Can it be not done with executions itself? because, point having multiple executions is running them on choice is it not?Mesdemoiselles
And also it is running other tests also...not sure where the problem isMesdemoiselles
@Mesdemoiselles Answer updated. Multiple execution is not for executing them on choice. It is to execute the plugin multiple times.Zebulen
I tried with eclipse run as maven build, in the goals section I've given "clean test", in profiles I've given resource-tests. Build was success but it gave [INFO] tests skipped.Mesdemoiselles
due to the exclude mentioned in configuration level of plugin it is excluding all tests...but if I remove that it is executing all.Mesdemoiselles
@Mesdemoiselles Yes you need to remove that configuration. I edited my post accordingly.Zebulen
But if I remove that, all tests are running but I don't want all tests to be run, I just want the classes included under the particular execution run.Mesdemoiselles
I'm just surprised this answer hasn't got at least 200+ yetOhmage

© 2022 - 2024 — McMap. All rights reserved.