How to separate Unit Tests and Integration Tests for different levels of maven lifecycle - maven

Here is my plugin configuration from pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>run-unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<skipITs>true</skipITs>
<skipUTs>false</skipUTs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>run-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
<configuration>
<skipITs>false</skipITs>
<skipUTs>false</skipUTs>
</configuration>
</plugin>
I have 2 test classes HelloTest.java and WorldITest.java.
When I run mvn clean test i want it to execute only HelloTest. But when i execute mvn clean integration-test, then I want both HelloTest and WorldITest to be executed.
But the problem is that it executes the Integration test also in mvn clean test command.
PS: Original code was taken from the discussion : Prevent unit tests in maven but allow integration tests

Related

Running spock integrations tests in its own folder in maven

I am trying to only run integration tests in maven with mvn. I have my tests in src/it/java for java integration tests and src/it/groovy for Spock integration tests. When I run mvn failsafe:integration-test, only the java integration tests in src/it/java are run.
This is my pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-integration-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
<source>src/it/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<testSourceDirectory>src/it/groovy</testSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<configuration>
<testSources>
<testSource>
<directory>src/it/groovy</directory>
<includes>
<include>**/*IT.groovy</include>
</includes>
</testSource>
</testSources>
</configuration>
<executions>
<execution>
<id>groovy-compile</id>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
Any idea what I am missing here?
It looks like the issue is that your running only failsafe:integration-test. I am able to run groovy and java tests using the command:
mvn verify
In the failsafe docs it suggests to use the verify phase here - http://maven.apache.org/surefire/maven-failsafe-plugin/index.html
If mvn verify still doesn't run your groovy tests, check your_project/target/test-classes/path_to_your_test for expected class files and make sure that lines up with the includes option in the pom.

Maven Config Plugins running twice

I have the following pom config. I added the cobertura plugin, and now pmd, cpd, findbugs, and test are running twice.
I understand that is because of my "phases" config, but I don't understand how can I achieve the following:
What I want is before I commit to the repo, build my app and check for pmd errors, findbugs errors, check my tests, and check my cobertura.
How can I achieve this? I am used to run "mvn clean package" before commit. It's that ok?
Here's my config:
...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<linkXref>false</linkXref>
<rulesets>
<!-- Custom Ruleset -->
<ruleset>codequality/pmd.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>pmd</goal>
<goal>cpd</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<check>
<haltOnFailure>true</haltOnFailure>
<branchRate>70</branchRate>
<lineRate>70</lineRate>
<totalBranchRate>70</totalBranchRate>
<totalLineRate>70</totalLineRate>
<packageLineRate>70</packageLineRate>
<packageBranchRate>70</packageBranchRate>
</check>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any maven plugin has a default execution phase. In this case, the plugins that you apply are executed in the verify phase (pmd-plugin). But you are defining it to run also in the compile phase. Removes the phase tag and lets it run in the verification phase.
<execution>
<!--<phase>compile</phase>-->
<goals>
<goal>...</goal>
...
</goals>
</execution>
Finally, the good practice to validate your project before a commit is to run:
mvn clean verify

How to debug system test using maven?

I wrote a system tests (using junit), to test my work flows.
How I did run this is,
mvn clean install -Psystemtest
This will run all the systemtest, what I have.
Now I wanted to debug only one systemtest at a time.
Is there any way to do this?
This is the 'failsafe-plugin'
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/systemtest-testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</execution>
</executions>
</plugin>

How do include JUnit test cases as a part of Maven build?

I have existing JUnit4 test cases which I run from Eclipse and as a part of Maven. Now I am looking to perform stress tests by leveraging them. I noticed Maven is not packaging them as a part of the build. How do I go about it?
This will attach the tests (and their sources) in a separate jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
<execution>
<id>attach-test-sources</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
Afterwards you can use them as a dependency by using the type test-jar.

maven-failsafe-plugin is not executing my integration tests

Bellow a part of my actual pom.
Testng tests for integration tests have been assigned a "integration" group in the #Test annotations.
To do little test I did not exclude the "integration" group during the test phase.
When building using for example mvn verify or mvn install the integration tests get executed in the test phase, but not the verify or integration-test phase.
The number of tests run remains 0. Somehow they are not picked up. Anyone have an idea on what might be wrong?
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.1</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.1</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<skip>false</skip>
<excludedGroups>unit</excludedGroups>
</configuration>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
<configuration>
<skip>false</skip>
<excludedGroups>unit</excludedGroups>
</configuration>
</execution>
</executions>
<configuration>
<skip>false</skip>
<excludedGroups>unit</excludedGroups>
</configuration>
</plugin>
Update: Adding TestNG as a dependency to the failsafe plugin does not
help
maven-failsafe-plugin by default includes only files matching following patterns:
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
How did you name your test class(es)?

Resources