Cobertura not recognizing tests - maven

I have added the dependency but for some reason Cobertura decided against loading any of my test classes from src/test/. It just loads the classes from src/java/ and hence doesn't show the code coverage or anything. I checked the packages (all the classes, tests are in the same package) and the dependencies. Any help, pointers??
Here is my cobertura dependency plugin under maven-shade configurations:
<cobertura.version>2.5.2</cobertura.version>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
</plugin>
I also have the same thing under maven-site plugin

Your structure is not mavenized, unless you have overridden the default maven configuration. It should be src/main/java and src/test/java.
Also check that you have followed surefire plugin (the plugin that runs tests) conventions (e.g. *Test.java) or have overridden configuration see surefire inclusion-exclusion for all of the default patterns accepted.
Verify that mvn clean test has run your tests, i.e. look for "Tests run: 52, Failures: 0, Errors: 1, Skipped: 0" in the output.
Verify that mvn cobertura:cobertura also runs your tests and produces a index.html in /target/site/cobertura/ (open this file to ensure that it includes all of your classes). Also check for the presence of /target/cobertura/cobertura.ser.

Related

Sharing common Springboot integration tests with jar?

Can I have a jar containing Springboot integration tests & use this jar in other modules to execute the common integration tests?
As stated here:
Maven projects already define a standard for a project's tests. And in
my opinion it doesn't make sense to make a project dependent on its
tests--if anything, tests would be dependent on the module under test,
since tests use the module, but not vice-versa.
Though if you wish to do this anyway, to answer your question: yes it is possible. As described here:
There is a new way of running a test in Maven from another jar. from
maven-surefire-plugin version 2.15 you can tell Maven to scan your
test jars for tests and run them. You don't need to extract the tests
jar. Just add a dependency to your test jar like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<dependenciesToScan>
<dependency>test.jar.group:test.jar.artifact.id</dependency>
</dependenciesToScan>
</configuration>
</plugin>

Maven skip specific test [duplicate]

In my maven project I have a number of modules. Is it possible to turn off running unit test for some modules via command line options?
My project takes about 15 mins to run through all unit tests. I would like to speed up the overall build by running just the unit tests in the module I am working on. I do not want to go in and edit each individual pom.xml to achieve this.
I have tried a solution outlined here: Can I run a specific testng test group via maven? However the result is a lot of test failures in modules that I want to skip. I suppose 'group' is not the same concept of module?
To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.
If you need more fine grain control of running a subset of tests for a module, look into using the Maven Surefire Plugin's test inclusion and exclusion capabilities.
To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. Take for example the following POM segment:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude>${someModule.test.excludes}</exclude>
</excludes>
<includes>
<include>${someModule.test.includes}</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<someModule.skip.tests>false</someModule.skip.tests>
<skipTests>${someModule.skip.tests}</skipTests>
<someModule.test.includes>**/*Test.java</someModule.test.includes>
<someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
</properties>
With a POM like the above you can execute tests in a variety of ways.
Run all tests (the above configuration includes all **/*Test.java test source files)
mvn test
Skip all tests across all modules
mvn -DskipTests=true test
Skip all tests for a particular module
mvn -DsomeModule.skip.tests=true test
Only run certain tests for a particular module (this example includes all **/*IncludeTest.java test source files)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" test
Exclude certain tests for a particular module (this example excludes all **/*ExcludeTest.java source files)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test
Found a way to exclude on command line:
# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*
This is from: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/
…and if you like to pass the parameter to maven release plugin in Hudson/Jenkins you have to use
-Darguments=-DskipTests
to get it work.
If you want to use Maven profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
you might want to make it work doing something like this:
Skipping tests in some modules in Maven
I don't know if there is a supported command line option that does the same.
You also might try using environment properties directly, something as per this doc page:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html
i.e. something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${moduleA.skipTests}</skipTests>
</configuration>
</plugin>
then using mvn -DmoduleA.skipTests=false test to test that one module.

Why maven-surefire-plugin needs to be defined for the tests to be run

If I call mvn install for example, the test phase being located before in the hierarchy should be executed. However it seems not to be the case.
I actually need to specify it manually: mvn test install in order for my tests to be executed.
OR
Respecifying the surefire plugin in the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
</plugin>
The test phase is supposed to call the maven-surefire-plugin which is a core plugin.
This behavior doesn't seem consistent to me. Am I missing something ?
EDIT: I am using Maven 3.2.1
The Maven version I am using (3.2.1) embeds the version 2.12.4 of the maven-surefire-plugin.
My project was a study case and had no test classes in it.
The 2.4.2 version of maven-surefire-plugin must fetch by default the Test reports even if no test class is encountered. The 2.12.4 seems not to behave this way.
When I was specifying the maven-surefire-plugin, I was overriding the plugin definition to the version to 2.4.2.
My apologies for having taken your attention for such an edge case and also with a lack of details.

How to defer Maven test goal after deployment?

I'm using the JBoss and WildFly Maven plugin to deploy my applications.
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.0.Beta1</version>
</plugin>
</plugins>
The problem that I have found is that if I have some Junit test in my project they are executed before application deployment, leading to a test with an inconsistent application state. Is it possible to configure somehow this plugin to kick-in before the test phase ?
Thanks
This plugin by default executes alongside the package phase and you'll probably have issues attempting to run the plugin before your application is packaged. As an alternative, you could override the maven-surefire-plugin to run your tests in a later phase, such as integration-test, which would be executed after your application has been packaged.
A practical example can be found on this Maven tutorial where it's show how to include failsafe plugin and bind it to the executions of the integration-test and verify phase. This way tests which are engineered as integration test (e.g ending in *IT) will execute only during the integration-test phase.

Maven: skip test but still fails on surefire

I dont know if this is a question or suggestion. But I am going to ask it as a question cos may be i am doing something wrong.
My problem was that I wanted to skip tests in maven build, In eclipse plugin I checked the Skip Test option in configuration when running maven. It was still failing on the Test surefire plugin as it couldn't download the version 2.4.3 (even though my previous maven project used a 2.7.2 and it was already there in my repo) So i tried with skipping tests and it still failed.
I configured my POM to use the 2.7.3 plugin of surefire which i already had and it went forward only to say Skipping Tests. Now, my confusion is that when it was already going to SKIP the test part why bother going into the download and confirming if the plugin for surefire is there or not. Just Skip it I say..
well, Is that the normal behavior of maven that when you skip something the plugin is still downloaded as if you are going to use it. Or was I doing it wrongly that made it download it.
May be because there was something new called "Effective POM" and it contained a listing of surefire plugin 2.4.2 in the plugin management area, when i imported my maven project in eclipse using the m2eclipse and i couldnt edit the Effective POM. I had not seen this before in the NetBeans when making the maven project.
In order to work i added an unnecessary surefire plugin entry in my build profile and skipped the tests there as well and added the version that I had in my repo already. I only did this so that my project can be built under eclipse as well. other wise my project works in NetBeans and simple command line without any issues.
Any comments!!
I think maven should be able to see first the SKIP part and then proceed into the usage of the plugin.
Syed..
Based on what you mentioned you didn't understand the difference between using a plugin and configuring the plugin. In this case you are using the plugin (it's in your build area or as you already figured out coming via super-pom). Furthermore you are trying to skip the tests despite the fact that maven-surefire-plugin in version 2.7.3 does not exist.
The configuration parameter skip will not execute the tests as well as not compile them. If you wan't just ignore the tests for a limited time you can use the "skipTest" parameter which in contradiction to skip will continue to compile the tests.
I would recommend to use a pluginManagement section of your project or your parent pom to define the version of the maven-surefire-plugin (which in the meantime exists in version 2.12)...
The following snippet add the pluginManagement part to a pom which controls which version of the maven-surefire plugin will be used.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
..
</build>
After you inserted that into your project pom the output effective pom should give you the version...otherwise you might need to update the project configuration first.
Compiling and running tests is part of the default lifecycle of maven. Since maven uses surefire to do run tests, it needs to download surefire. skipTests is not a maven configuration, but a surefire plugin configuration. The plugin determines that maven needs to skip tests.
As for 2.4.3 of surefire plugin, it is a valid version, but will work with 2.0.6 version of maven. Most likely you are using a newer maven, but, for some reason have the super pom of the older maven version on your system.
skipTest doesn't tell maven to skip the test lifecycle, it tells the surefire plugin not to run them. The plugin is still part of the lifecycle (it just does nothing when it's called). Hence your error.
If you want to NOT have surefire at all, you need to define your own packaging (since surefire is part of the standard jar packaging lifecycle) - which is a lot more work than just choosing a version of surefire that works for you (add a section with the right in your section).

Resources