Running a JUnit4 Test Suite in Maven using maven-failsafe-plugin - maven

I've got a JUnit 4 test suite that contains a number of test classes in the order they need to be run (our Integration tests need to be run in a certain order).
If I use the maven-failsafe-plugin without any configuration it will run the test but not in the correct order. However, If I set the plugin to run the test suite no tests are run.
Is it possible to run a test suite using the failsafe plugin? if so, where have I gone wrong!!
The code is below:
#RunWith(Suite.class)
#SuiteClasses({
TestCase1.class,
TestCase2.class,
...
TestCaseN.class,
})
public class IntegrationSuite {
//Do Nothing.
}
and from pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.9</version>
<configuration>
<includes>
<include>IntegrationSuite.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Thanks :)

Failsafe plugin supports runOrder (click) parameter since version 2.7 (quite recent). There aren't many options, you cannot specify the order explicitly, but you can set it to "alphabetical" and rename your test classes to reflect the run order.
May I also say on the occasion that the fact that test depend on each other is (test) code smell; it's not good, as it is a short path to developing an unmaintainable set of tests and abandoning it finally when its complexity skyrockets above human comprehension. Plus it may fail to expose bugs, as it is a result of one chosen execution path.
BTW, I prefer to include tests like this, with a double asterisk:
<includes>
<include>**/IntegrationSuite.java</include>
</includes>

maven-surefire-plugin can also be used as below code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<includes>
<include>**/IntegrationSuite.java</include>
</includes>
</configuration>
</plugin>

Related

How to run before and after maven test

mvn test
I am looking to initialize certain resources before all test cases executed using maven test and also to destroy them after all test cases are executed.
I have looked into jUnit #BeforeClass, #AfterClass, #Before and #After, but none of them are helpful.
I have tried to use maven life-cycle phases i.e. pre-integration-test as below, but even in this case the expected Test case (TestPostgresqlEmbedded) doesn't get executed first.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<executions>
<execution>
<id>test-init</id>
<configuration>
<runOrder>alphabetical</runOrder>
<includes>
<include>**/TestPostgresqlEmbedded.java</include>
</includes>
</configuration>
<phase>pre-integration-test</phase>
</execution>
<execution>
<id>test-all</id>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
</execution>
</executions>
</plugin>
How can I achieve this ?
Use the integration-test phase for tests with databases. Then you have pre-integration-test to set up your database resources, and post-integration-test to destroy them.

mvn integration-test command pulls in unwanted unit tests for execution

When I run mvn test it executes unit tests only but when I run mvn integration-test it executes both unit test and integration test even after configuring the maven-failsafe-plugin and excluding the *Test.java file. Not sure what I am missing here. Also worth mentioning that I have not put in maven-surefire-plugin in my pom.xml. Not sure if that is creating this problem. Please guide.
pom.xml
<!-- Integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Unit Test Class File:
com.study.jenkins.ut.MyUnitTest.java
Integration Test Class File:
com.study.jenkins.it.PageIT.java
The maven-surefire-plugin is part of the lifecycle, which is always bound to the test-phase for Java projects. Calling integration-test means that all lifecycle-phases up to the integration-test phase are executed. So the MyUnitTest will always be executed (which is a good thing).
Your includes/excludes have no effect, these are already the defaults for the maven-failsafe-plugin, see http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#includes

How to integrate cucumber-jvm with maven build

i'm trying to put some bdd into my eclipse plugin project, but can't figure out how to run my integration tests during maven build fase. To write my tests i'm using SWTBot framework.
I already did the feature generation fase, and setup my tests. How do i setup my pom to run my integration tests?
I use below configuration and run mvn clean verify. If you don't want to run tests in parallel, remove parallel, perCoreThreadCount and threadCountClasses tags.
Make sure to update the regular expression to match your test naming convention <include>**/Run*.java</include>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>acceptance-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<outputEncoding>UTF-8</outputEncoding>
<parallel>classes</parallel>
<perCoreThreadCount>true</perCoreThreadCount>
<threadCountClasses>10</threadCountClasses>
<argLine>-Xmx1024m</argLine>
<argLine>-XX:MaxPermSize=256m</argLine>
<includes>
<include>**/Run*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

Running a plugin goal before the default one

TL;DR: Using maven, I want to run a plugin goal at the beginning of the test phase, before tests actually run. What would be a clean way to do it?
I want to print a message just before the tests actually run. Hence I want to use the echo goal of the echo plugin at the beginning of the test phase (to tell the user that if every tests fail, he'd better have a look at the README since there's a test environment he should set up first)
Attempt n°1
A simple approach could be to run this plugin in the previous phase, process-test-classes.
It works, but it doesn't seem semantically correct to bind this task to this phase...
Attempt n°2
According to Maven documentation, When multiple executions are given that match a particular phase, they are executed in the order specified in the POM, with inherited executions running first., so I tried to set explicitly the surefire plugin:
...
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>maven-echo-plugin</artifactId>
<version>0.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
<configuration>
<echos>
<echo>*** If most tests fail, make sure you've installed the fake wiki. See README for more info ***</echo>
</echos>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
...
But tests run before my message is printed.
So, to put it in a nutshell: is there a way to reach my goal, or should I stick to the "process-test-classes solution" even though it seems a bit "hacky"?
Thanks!
As #khmarbaise said, your solution is still hacky, because whole test looks like Integration Test and should be processed by Failsafe Plugin. Failsafe has nice phase pre-integration-test for testing fake wiki etc :)
Based on Guide to Configuring Default Mojo Executions this works for me:
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>maven-echo-plugin</artifactId>
<version>0.1</version>
<executions>
<execution>
<id>1-test</id>
<phase>test</phase>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
<configuration>
<echos>
<echo>*** If most tests fail, make sure you've installed the fake wiki. See README for more info ***</echo>
</echos>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
<execution>
<id>2-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
This is very odd for me ;)
I have two plugins with executions bound to generate-sources, one listed first in the list of about 6 plugins and the other listed last. However, the one listed last (which depends on the one listed first) always executes first.
How can I execute several maven plugins within a single phase and set their respective execution order?

How to execute multiple JUnit test suite at a time..!

I have written a multiple JUnit test suite for running multiple test cases.(multiple JUnit test like AllTest1.java/AllTest2.java is requirement of my web project)
Now, I want to run my multiple test suite classes (AllTest1.java/AllTest2.java) at a time means on same build time.. for this I used maven-surefire-plugin but not able to execute both same time I have used many logic to execute but not successed:(..Is there any way to execute both test-suite parallely .
Any help will be appreciated.
This is my current maven-surefire-plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1/version>
<configuration>
<includes>
<include>**/AllTests1.java</include>
</includes>
<excludes>
<exclude>**/AllTests2.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
This looks indeed like integration tests and unit tests or a combination of both. For such purposes i would suggest to use the maven-failsafe-plugin to run the integration tests which should follow the naming schema:
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
Furthermore the usual unit tests will be executed by the maven-surefire-plugin which should follow the following naming schema:
<includes>
<include>**/*Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
Furthermore you should avoid using test suites, cause based on the naming schema it can be distinguished if it's a unit- or integration tests.
After you changed to the above schema you can run the unit tests by
mvn test
running the integration tests by using:
mvn verify

Resources