Reputation: 6070
I'm hoping someone can enlighten me on how junit, maven-surefire-plugin or maven controls how many times to run a test, or if there's something in config or property files somewhere that I failed to find/lookup/set that controls the number of times.
For some reason, when I run my tests in maven, they get executed twice. I see it in the console output. I think the maven-surefire-plugin might be part of the reason but I'm not sure. I run the command:
mvn -Dtest=TestClassBlah test
I see the log msgs in the console that it starts up, runs the tests, gives a summary, then runs it again for whatever reason:
(Log file sanitized and abridged. Pls excuse any typos from the sanitizing)
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestClassBlah 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
<snip snip>
<snip snip>
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.blah.blah.TestClassBlah
<bunch of INFO and other msgs>
<bunch of INFO and other msgs>
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.465 sec
Results :
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-surefire-plugin:2.11:test (tests) @ TestClassBlah ---
[INFO] Surefire report directory: /dir/somewhere/for/me
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.blah.blah.TestClassBlah
<bunch of INFO and other msgs again>
<bunch of INFO and other msgs again>
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 8.767 sec
Results :
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.799 s
[INFO] Finished at: 2014-08-07T18:54:27+01:00
[INFO] Final Memory: 19M/183M
[INFO] ------------------------------------------------------------------------
I can attach my pom.xml file if needed, although, it just lists our plugins, dependencies, repos, etc. It doesn't seem to list number of times to run tests or suppress output from surefire-plugin.
We use the maven-surefire-plugin to post our results in Jenkins. This is my first time using these technologies so I don't know all the tricks of the trade yet. Thanks in advance for any help you can give me.
UPDATE: Here's the pom. Sorry it's kinda long.
<modelVersion>4.0.0</modelVersion>
<groupId>GenericGroupName</groupId>
<artifactId>TestClassBlah</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TestClassBlah</name>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.genericname</groupId>
<artifactId>analytics</artifactId>
<version>1.5-RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.11</version>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<executions>
<execution>
<id>tests</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>bitExpert-nexus-releases</id>
<name>bitExpert nexus</name>
<url>http://10.196.32.21:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
Upvotes: 2
Views: 1401
Reputation: 2223
You have an extra execution of maven-surefire-plugin specified in your pom. Maven has a default execution of Surefire that's running as well. If there's a tag under the of Surefire in your pom, you can move that up under the tag for maven-surefire-plugin and it will be applied to the default execution of maven-surefire-plugin. Then you can delete the extra execution.
If you want Surefire to run one way on desktops and another way in Jenkins, that's a good use case for profiles.
Edit: after looking at your pom this should definitely work and only run surefire once:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
You might be able to remove the entire <configuration>
tag depending on how test classes are named--Maven normally interprets every class under src/test/java whose name starts with Test or ends with Test or TestCase as a test class. If all your test classes already follow that convention, then configuring Maven to hoover up every class as a test class is just redundant.
Upvotes: 2
Reputation: 1304
The simplest answer to this is that the surefire plugin is specified in your pom (and/or its parents) with more than one execution, or there are two plugins that perform the same action (I haven't seen it with surefire, but sometimes another organization publishes the same artifact under a different name, developers get confused when doing a search and both end up in the project). If you have parent POMs, executions are merged by their id, if the ids differ, there will be multiple executions.
You snipped the important part of the log, which is the line "--- maven-surefire-plugin:2.11:test (tests) @ TestClassBlah ---", which includes the plugin artifact ID (maven-surefire-plugin) and execution id (tests). If these lines differ between the two executions, you have your answer.
Upvotes: 2