This question already has answers here:
Maven does not find JUnit tests to run
(34 answers)
Closed 7 years ago.
I am struggeling on executing all junit tests by maven test.
There are 57 Tests in 10 classes but somehow maven executes only
12 Tests in 6 classes. The ignored classes are in the same folder
as another class, beeing executed.
However, when run my test source folter as JUnit Test it does execute
all tests.
Funny thing: I copied a test, which had been executed, but even the copy
is ignored by maven.
Any help will be appreciated.
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<append>true</append>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
By default, it is the maven-surefire-plugin that is launching the unit test and it includes only classes whose name match a condition.
This is documented in the includes parameter of this plugin. Quoting:
When not specified and when the test parameter is not specified, the default includes will be
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
Therefore, you need to make sure all of your test classes respect this naming convention. Otherwise, you need to override this default configuration to suit your needs.
Related
Is it possible (and how) to substring a variable in the pom.xml, or the properties that uses this variable?
My scenario:
I have a swing application that shows a kind of version in its footer.
This version is read from a properties file.
The properties file only have a reference for a maven variable, as:
version=${git.branch}
In my pom.xml, a have a plugin that looks for the branch name, and write it in the "git.branch" variable.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/version.properties</exclude>
</excludes>
</resource>
</resources>
</build>
But now we are using a prefix for the branch names, and this prefix shouldn't be deployed with the application version.
I had branches like:
v123
v124
v125
Now I have branches like:
b_04_v123
b_04_v124
And i want maven to get only the value "v124" of the string "b_04_v123", the prefix is fixed, aways like "b_NN_".
NOTE: No, it's not possible to change the branch names, as other departments uses them with scripts and automation.
you can use org.codehaus.groovy.maven plugin:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.setProperty("version2","${version}".replace('b_04_', ''))
</source>
</configuration>
</execution>
</executions>
</plugin>
and now you can use version2 :
version=${version2}
I solved it inside the java code, I already had a code reading the properties file, just added the substring there.
It isn't a beautiful maven solution, but worked. I could have done this since the beginning.
But, if someone could tell how can I substring a variable in my pom.xml it would be great, although I don't need it anymore (or with the same urgency) I'm still curious.
I stumbled upon this question myself and the other provided answers did not work for me. So I created my own Maven plugin.
It can search and replace other variables or normal text using regular expressions and so create a new variable.
For your case it would be something like this:
<plugin>
<groupId>io.github.1tchy</groupId>
<artifactId>variable-search-replace-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<text>${git.branch}</text>
<search>^b_\d\d_</search>
<variableName>old.version</variableName>
</configuration>
</execution>
</executions>
</plugin>
Then you can simply use it as a normal property: version=${old.version}
The plugin can also define a replacement text: check out its documentation!
I have a Maven Java project in which I added to the pom:
<build>
....
<plugin>
<!-- adding second test source directory (just for integration tests) -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${plugin.build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>add-integration-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration-test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/integration-test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</build>
InteliJ recognized my java and resource folders under integration-test as a code folder, but Eclipse doesn't.
Is there any way that eclipse adds these folders as code folders when the project is imported?
Try to right click on your folder in Project Explorer select Build Path option in context menu and later click Use as Source Folder in menu which appears after choosing Build Path.
I suggest not using your own directory layout with Maven since this will cause many problems and you always have to configure around it. Just stick to the standard.
Separate integration tests and unit tests not by their source folders, but by their name.
Put all tests in src/test/java. You don't have to configure anything at this point, this path is taken by default.
Call integration tests IT*.java and unit tests UT*.java.
They can be run separately because maven-surefire-plugin executes unit tests and maven-failsafe-plugin executed integration tests. You can define filename patterns for identifying the test classes.
You could also create profiles for running only UTs or only ITs.
<project>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<includes>
<include>**/UT*.java</include>
</includes>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>**/IT*.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
Further reading: http://tomaszdziurko.pl/2013/01/running-unit-tests-integration-tests-separately-maven-testng/
There is also a interesting article about the correct usage of integration tests here: http://zeroturnaround.com/rebellabs/the-correct-way-to-use-integration-tests-in-your-build-process/
I recently stumbled upon a simple way to parallelize the execute of tests via jUnit by specifying the following in a java project's pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>classes</parallel>
</configuration>
</plugin>
I've discovered that there are 2 test-classes (let's call them "badtestclass1" and "badtestclass2") that keep getting penalized by this parallel execution due to the way the tests in them have been written. Ideally, I would refactor those test-classes to behave better, but in the interim, I was wondering if there's a nifty way to "exclude" these specific classes from being executed in parallel. Basically, is there a way to execute everything else in parallel and then these 2 in sequence (or the other order, doesn't matter). Would something like the following work?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>classes</parallel>
<excludes>
<excludesFile>badtestclass1</excludesFile>
<excludesFile>badtestclass2</excludesFile>
</excludes>
</configuration>
</plugin>
#polaretto's answer suggests the jcip #NotThreadSafe annotation, which works with the surefire plugin in a build, but does not work with command line mvn test. #patson-luk's answer is on the right track, unfortunately by excluding the "bad test" in the default configuration, it remained excluded and was not run in the separate <execution>.
I managed to get this working using the following configuration:
For JUnit 5, these are sufficient
In my src/test/resources/junit-platform.properties file (or you can pass as command line parameters):
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
At the top of my not-thread-safe class (instead of the jcip annotation):
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
#Execution(SAME_THREAD)
class SingleThreadedTest {
// ...
}
For JUnit 4, modify the accepted answer as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<!-- Skip default, define executions separately below -->
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>single-thread-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- Not thread safe, run separately -->
<includes>
<include>**/SingleThreadedTest.java</include>
</includes>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<threadCount>1</threadCount>
<skip>false</skip>
</configuration>
</execution>
<execution>
<id>multi-thread-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/SingleThreadedTest.java</exclude>
</excludes>
<forkCount>4</forkCount>
<reuseForks>true</reuseForks>
<parallel>all</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
You can annotate the classes you don't want parallelized with jcip #NotThreadSafe and leave the surefire configuration as it was in your starting example. This way, whenever surefire finds an annotated class it just executes it in a single thread. It's explained right here in the "Parallel Test Execution and Single Thread Execution" paragraph.
Exclude those 2 tests in the original test phrase and then create a new execution with those 2 classes running in single thread? :)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>path/to/your/class/badtestclass1.java</exclude>
<exclude>path/to/your/class/badtestclass2.java</exclude>
</excludes>
<parallel>classes</parallel>
</configuration>
<executions>
<execution>
<id>single-thread-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>path/to/your/class/badtestclass1.java</include>
<include>path/to/your/class/badtestclass2.java</include>
</includes>
<threadCount>1</threadCount>
</configuration>
</execution>
</executions>
</plugin>
I'd like to remove a dependency for a unit test. I found how to do it in this answer.
But I'd like to remove a dependency for only one specific test, not for all my tests. Is there a way to do that?
Not by using one Surefire execution.
You will have to define two executions of the Surefire plugin: one containing the full Classpath for most of the tests, and one containing the specialized Classpath for the single test that requires it.
Follow the Surefire plugin's documentation: http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
You'll have to create two executions, and bind them both to the test phase. Use the following example as a skeleton (you'll have to adjust the include and exclude patterns, as well as the excluded Classpath artifact):
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>full-cp</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/Test*.java</include>
</includes>
<excludes>
<exclude>MyFancyTest.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>special-cp</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>MyFancyTest.java</include>
</includes>
<classpathDependencyExcludes>
<classpathDependencyExcludes>excluded-artifact</classpathDependencyExcludes>
</classpathDependencyExcludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
I am trying to get a JBehave story to execute in Maven it is completely ignoring the JBehave plugin. I've spent several hours using different configurations but it looks like the plugin isn't being executed at all. Any recommendations/tips would be appreciated!
All my JBehave classes live in:
src/at/java
Relevant parts of my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/at/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<executions>
<execution>
<id>run-stories-as-embeddables</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Best is to change the location of your test classes to src/test/java and change the name of the stories based on the documentation of JBehave.
JBehave running with maven follow the Maven rules for location of code and text artifacts.
For test scope you must put them in src/test/java and src/test/resources. For compile scopes is src/main/java and src/main/resources.
With JBehave with maven you could use two scopes (test or compile), you just need to set which one you want in the plugin configuration, so you choose where to put your artifacts. it defaults to compile.
In your case you are adding a new test source so you must set the scope to test:
see detail here.
Maybe the jbehave-maven-plugin could not find the compiled test classes (scenarios) because it looks in the wrong classpath.
Please look at your target directory and search the embeddable classes -> target/classes or target/test-classes?
To solve the problem i must set the scope of jbehave-maven-plugin to test in the configuration of my project pom.xml.
here is a example
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<executions>
<execution>
<id>run-stories-as-embeddables</id>
<phase>integration-test</phase>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
<configuration>
<scope>test</scope>
<includes>
<include>**/*Scenarios.java</include>
</includes>
<ignoreFailureInStories>true</ignoreFailureInStories>
<ignoreFailureInView>false</ignoreFailureInView>
</configuration>
</execution>