I have used Maven for a while. In my previous projects, i did not create pom.xml from scratch, and I've seen Surefire plugin always already added to POM.
At first I thought Surefire is a must for Maven to be able to search for test java files to run, however even after I remove Surefire plugin from POM, Maven is still able to find the test cases under src/test/java.
So is the purpose of Surefire to add additional features such as in:
https://maven.apache.org/surefire/maven-surefire-plugin/
but not absolutely necessary to run "mvn test"?
Thanks
Very first line you doc you provided says:
". It generates reports in two different file formats:"
And reports are very important, I assume you must have seen reports after running test to check coverage and other stats.
You have answer somewhere in your question itself :)
I was also searching for reasons why so many plugins required such as Sure-fire and fail-safe..
found that we need surefire for "test" phase to execute the UNIT TEST of an application
which is the least minimal activity we do to after compiling our code therefore surefire is omnipresent.
Related
I have a plugin that is supposed to send files to a database regardless of test failures. I tried setting the fail-at-end setting but the plugin still didn't run. ignoring failures altogether doesn't seem to be an option for me in this case because then faulty jars would be deployed to the artifact repository. I can use this plugin for as a separate command line script but it would be optimal if it could be put in the pom.
The tests need to be run before the plugin does, because the plugin gets the test report files.
I am aware that there are similar questions to this that have been asked before, but they don't seem to help me so I was wondering if someone has similar advice.
If the plugin has to run after the tests, the tests just can't fail the build.
Maven Surefire plugin has an option: maven.test.failure.ignore to run the tests but keep execution
See Here a full description of Test MOJO.
This should be the first bet.
Another option is to build just like this, but run the plugin "externally" (a kind of two steps build). Run maven install or whatever you need, and then (separately)
mvn <group>:<artifact>:<version>:<mojo of your plugin>
try mvn your-goal -DskipTests - will ignore testing.
I want to know how to link production code and test code, i.e., I'd like to answer this question: which product codes are the targets of this test code?
I'd like to do this automatically.
My project uses maven and I have used SonarQube and the source codes are written in Java.
If it's needed, I will try any other tools.
How can I link production codes and test codes?
Please let me know how to do it.
What you want to do is effectively getting the Coverage of your tests, an answer to the question "What lines/branches of my code are covered by my tests?".
Maven and SonarQube are perfectly suited for this, the only thing you need to add into the mix is Jacoco. A good explanation for the configuration of Jacoco/Junit is here. Jacoco is an agent that gets added to the execution of your tests and which monitors them, analyzing which lines/branches have been executed (covered) and which have not.
The important part is to configure the jacoco plugin and the surefire/failsafe plugin(s) (last one is for integration tests) to use jacoco. This will generate jacoco report files, which then can/will be read by SonarCube during the sonar:sonar goal (you might have to set the path to these files either in your maven pom.xml as a sonar property or directly in the SonarQube server properties, both work fine).
You can test it step-by-step, first getting jacoco to run, since it already creates nice html reports. Reading the reports into SonarQube is the easier part then.
I am just looking at the cobertura maven plugin and I wasnt sure if the following is possible
Instrument classes
Run junit tests
Generate Cobertura report without reinstrumenting classes and running tests
I have a multi module maven project and the coverage of the domain module is showing up as 0% even though its been used by every other module
I have tried different combinations of things but the coverage of my domain module always stays at 0%.
People have mentioned writing separate tests for the domain classes but i dont want to do this as you could easily write a tests to test a function that isn't actually being used anywhere within the codebase
Any pointers would be greatly appreciated
In order to do so you would have to execute the maven goals in the correct order so :
cobertura:instrument
test
goalToAskCoberturaToGenerateReport
But then comes the trouble : there is no such goal as cobertura:report, if you look at the documentation and source code of the maven plugin : The goal cobertura:cobertura is the only goal generating the report. I suspect it is as such because of some maven internal limitation.
So in short, given the state of the maven plugin it is not possible.
You might have a chance to manage what you want to achieve by executing cobertura from the command line.
For multi-module maven projects cross-module coverage seems not to be available off-the-shelf with Cobertura.
A solution using a mixture of maven and ant is described byThomas Sundberg:
http://thomassundberg.wordpress.com/2012/02/18/test-coverage-in-a-multi-module-maven-project/
See also this related question:
Maven2 Multiproject Cobertura Reporting Problems During mvn site Build
We recently had a bug caused by some code assuming that a classpath resource could be opened as if it were a regular file. This code was covered by unit tests which passed, because that assumption happens to hold both in Maven and Eclipse. But in production, that code was in a JAR in the service's WAR, and obviously didn't work.
We fixed the bug but I'm not quite satisfied because I can't see how to ensure that it doesn't happen again.
Unit tests are run using Surefire. This happens during mvn test and "test" occurs before packaging. But if you want to run tests after mvn package, you should use Failsafe.
Here's some very relevant documentation about configuring the classpath. By default, it will put ${project.build.outputDirectory} in the classpath, but you should be able to prevent that by setting classesDirectory to some other directory. Then you can add your own jar as either an additionalClasspathElements or perhaps a dependenciesToScan
could you please share your way of unit testing in eclipse ? Are you using surefire plugin, m2eclipse & maven, or only testNG eclipse plugin ? Do you combine these alternatives ?
I'm using testNG + maven surefire-plugin and I had been using the testNG eclipse plugin a year ago so that I could see the results in testNG view. Then I started using Maven, but when I do "maven test phase" using m2eclipse, there is only console output and surefire reports that I can check in browser and to choose what test suite, test, or test method can be set up only via testng.xml.
On the other hand, if you use only surefire plugin and you have some specific settings regarding classpath etc., that you rely on, then running tests via testNG eclipse plugin doesn't have to be compatible with your code. Using surefire plugin, the classpath is different - target/test-classes and target/classes - than using testNG plugin, that is using the project classpath.
How do you go about what I was just talking about?
Is it possible to synchronize "maven test" using m2eclipse and surefire plugin WITH testNG eclipse plugin and view ?
EDITED: I'm also wondering, why the Maven project ("Java build path") output folder is target/classes for src/main and src/test whereas surefire plugin makes two locations target/test-classes and target/classes
Thank you very much for your your opinions.
Lisa,
You can configure the TestNG Eclipse plug-in to "watch" a test-output directory. Point it to target/surefire-reports and you should see your TestNG view update itself a few seconds after a Maven build terminates.
I see two advantages of using the surefire plugin:
Relying on the eclipse plugin only
works that way when everyone in the
project uses eclipse
Surefire plugin
can run from build that are done from
the continuous integration server
("jenkins")
And then if you have larger more long running (performance) test suites, you probably don't want to "block" your IDE while they run.
I don't think it is true what you say with the different runtime classpath, I just checked myself both maven classpath and the one when test is run via testNG eclipse plugin and both are the same. I think that m2eclipse plugin takes care of it. It's the same even for junit testing.
So afaik, there is no restriction for using testNG eclipse plugin for development and surefire plugin for instance for continuous integration as Heiko Rupp mentions. At least I have never gotten any troubles with it.
Just use both as you like, cheers !