Maven Surefire with different file sets - java

I have two kinds of Unit tests (not integration test). Because of some strange behaviour with Spring Security I need to run first all normal tests, and later on the security tests.
I am using Junit (so I can not use any TestNG groups).
So what I have done is specify two sets of includes and excludes rules.
<excludes>
<exclude>**/*SecurityTest.java</exclude>
</excludes>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
and
<excludes>
</excludes>
<includes>
<include>**/*SecurityTest.java</include>
</includes>
That works if I replace them in my pom by hand so I can have normal or security tests. But of course I want that both kind of tests run in each build.
My first try was to have two complete maven-surefire-plugin configruation. But then maven take only the last of them in account.
My next try was to use two execution definitions, but then surefire seems to ignore the rules at all and run both kind of tests mixed.
So my general question is how to specify two file sets for maven surefire so that they will be executed each after another? And more specific how to specify two different file sets.
the configuration with executions
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<junitArtifactName>junit:junit</junitArtifactName>
<encoding>UTF-8</encoding>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
<argLine>-Xms256m -Xmx512m -XX:MaxPermSize=128m -ea</argLine>
</configuration>
<executions>
<execution>
<id>normal-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/Abstract*.java</exclude>
<exclude>**/*_Roo_*</exclude>
<exclude>**/*SecurityTest.java</exclude>
</excludes>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>security-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/Abstract*.java</exclude>
<exclude>**/*_Roo_*</exclude>
</excludes>
<includes>
<include>**/*SecurityTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

You haven't changed the default surefire execution which is bound to the test phase in the superpom, so it's still running with its default config. The id of that execution is "default-test". You'll either need to override it to unbind it from the test phase or else just use that id for one of your own executions.

Related

How to ignore the specific failed unit test result in Maven build?

I have some unit tests under Integration.java. I want Maven to ignore the test results of this class as they fail rarely (due to external server maintenance). But I don't want to ignore them from running as I need them for code coverage.
I've tried this configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>IntegrationTest.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
This seems to be working by ignoring the failed tests. But the problem with the above configuration is, it is ignoring the failed tests at other classes as well.
I want all the tests under Integration.java needs to be executed but It shouldn't have any impact on the build to determine success/failure.
But, the build should fail if any test cases fail under any other Test classes other than Integration.java
Like #khmarbaise has mentioned, It needs to be handled by maven-failsafe not surefire plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>*IntegrationTest.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
By using maven-failsafe-plugin I made sure the test cases run and for the good code coverage and the build will not fail even if the upstream is down.
Surefire by default includes all test classes whose name starts with Test/ends with Test/Tests/TestCase.
You can use excludes and includes parameters as per your need :
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<excludes>
<exclude>DataTest.java</exclude>
</excludes>
<includes>
<include>DataCheck.java</include>
</includes>
</configuration>
</plugin>

Maven Shade : include only specific artifacts and exclude all the rest

I am trying to achieve a very simple thing : Only include some artifacts in my build, while excluding everything else. I do not want to specify both include and excludes.
(Actually my problem with Maven Shade goes deeper than syntax, it is also about its logic, that puzzles me : I find the double paradigm (Inclusion + Exclusion) confusing because to me both are linked.)
I have been scratching my head for hours about this.
Although a few examples are provided here in the doc, I failed to find a working syntax in my case (I keep on getting everything included whatever I tried).
(I find the documentation pretty light IMHO and I can't find a thorough comprehensive reference for this plugin.)
I am sure there is a way to achieve this.
<filters>
<filter>
<artifact>**:**</artifact>
<includes>
<include>org/apache/logging/**</include>
</includes>
<excludes>
<exclude>all/the/rest/**</exclude>
</excludes>
</filter>
</filters>
also :
what is this /path/to/artifact syntax ? Does org/apache/logging refer to artifact org.apache.logging or am I mistaken.
I used the following approach as suggested in a comment :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
#here comes the interesting part
<artifactSet>
<includes>
<include>org.apache.logging.log4j:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
Could you please try this, it will include all the classes from org.apache.maven package
Sample ex:-
<profiles>
<profile>
<id>TestShadeProfile</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>org.apache.maven:*
</include>
</includes>
<excludes>
<exclude>*:maven-core</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
hope it will help you

Filter src/main/resources for integration testing

I am trying to filter a properties file in src/main/resources for testing only with a mock. Next revert back to filtering for the real class.
This is my setup:
src/main/resources/META-INF/ejb-jar.xml
in ejb-jar.xml
...
<ejb-link>${ejb.link.class}</ejb-link>
</ejb-local-ref>
src/main/resources/config.properties
in config.properties:
ejb.link.class = Mock
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>resources</goal>
</goals>
<id>filter-resources</id>
<phase>process-resources</phase>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/resources/config.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
In this example, this works fine for normal build. I would like to have a different filter for testing. Perhaps pull from src/test/resources, and when testing is done, the normal cycle is ran with the needed filter for production.
I can't seem to figure out the right sequence for execution for testing.
Any advice is appreciated.
Thanks,
Abe

Maven - run integration and unit tests

I have 2 sets of tests defined in Maven - integration-test and test.
If I run maven test - my tests run
If I run maven integration-test - both run
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<forkMode>always</forkMode>
<excludes>
<exclude>**/TC_Integration*</exclude>
</excludes>
<includes>
<include>**/TC_*</include>
</includes>
</configuration>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/TC_Unit*</exclude>
</excludes>
<includes>
<include>**/TC_*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
What's the best way to make both run all the time? I mainly want maven install to run both, but it's not.
Instead of trying to configure surefire to run both unit and integration tests, configure surefire to run just the unit tests and use the failsafe plugin to run the integration tests.
https://maven.apache.org/surefire/maven-failsafe-plugin/

Jenkins mixing up tests from different Java classes

We have a project with lots of tests running against a database. Many of the tests set up the database using #BeforeClass, and clean it out using #AfterClass, which works fine when we run the tests locally. When Jenkins runs them, it mixes up the order so tests from different classes are intermixed. For instance, A.TestAlpha, B.TestFoo, A.TestGamma. Then inevitably the tests fail because they don't have the correct setup.
My understanding of Jenkins is that it's supposed to run one class of tests at a time. Is there some config somewhere that might tell it to run the tests in this weird way?
(edit)
The same sort of problems occur running tests locally with mvn.
From mvn help:effective-pom
(project)
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<includes>
<include>**/When*.java</include>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
</configuration>
</plugin>
(module)
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/When*.java</include>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<includes>
<include>**/When*.java</include>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
</configuration>
</plugin>
Thanks to everyone who attempted to help.
The bug apparently lies in TestNG.
How to make sure TestNG runs methods on test classes in succession instead of interleaved?

Categories