Run JUnit tests in Maven without building and copying files [duplicate] - java

This question already has answers here:
How can I atomically run mvn tests (without rebuilding source code)?
(3 answers)
Closed 6 years ago.
I have a large Maven project which has several modules in it. When I want to run a JUnit test from one module I run 'mvn -Dtest=NameOfTest test' in the directory that contains all the modules. When I run this command Maven goes through each module and tries to compile it (though it's already compiled), which involves copying a bunch of files and adds to the total time of the test. It seems that the 'test' command for the Maven surefire plugin executes all the steps up to the test. I was wondering if there is a way to execute only the test step and not bother with all the attempted compilation and copying of files.
Here is some output from before the test starts:
[INFO]
[INFO] --- build-helper-maven-plugin:1.5:add-test-source (add-test-source) # module1 ---
[INFO] Test Source directory: <directory in module1 with some generated sources> added.
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) # module1 ---
[debug] execute contextualize
[INFO] Copying 108 resources
[INFO] Copying 1113 resources
[INFO] Copying 1 resource
[INFO]
It repeats this for each of the other modules. All told it takes a minute or two before it actually starts the test. Does anyone know a way to get the test to run without bothering with all the compilation beforehand? Please let me know if there's any more information I should provide.

If what you would like to do is to run just the test phase of the lifecycle without running all of the previous phases you could also call the goal that is bound to the test phase:
mvn surefire:test
or if you want to run just one test
mvn -Dtest=NameOfTest surefire:test

What is wrong with simply running the test from within the module the test resides in? That way, Maven will not attempt to build the other modules which you are not interested in.

Related

Create Jacoco report for external repository

I have a Spring-Boot maven repository. I can't alter pom.xml to add jacoco-maven-plugin. But I need a test coverage report for this repository.
So my idea was to execute the following command:
mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent org.jacoco:jacoco-maven-plugin:report
But in the logs I see:
[INFO] --- jacoco-maven-plugin:0.8.6:prepare-agent (default-cli) # backend ---
[INFO] argLine set to -javaagent:C:\\Users\\admin\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.6\\org.jacoco.agent-0.8.6-runtime.jar=destfile=C:\\projects\\my-project\\backend\\target\\jacoco.exec
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.6:report (default-cli) # backend ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
and no report is generated.
What should I do to add "missing execution data file"? And is it even possible to generate Jacoco report without adding anything to the pom.xml?
I figured this thing out. Turns out I needed to change the goal's order a bit.
Instead of:
mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent org.jacoco:jacoco-maven-plugin:report
Clean.
Verify.
Prepare Jacoco Agent.
Build report.
I need to do:
Clean
Prepare Jacoco Agent
Verify
Build report.
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent verify org.jacoco:jacoco-maven-plugin:report

Evosuite (maven) Unable to generate test / edit: tried eclipse plugin

I was testing evosuite (automation tool to generate junit test) using Maven plugin in Eclipse Luna, using JDK 1.8
I have followed the tutorial given in evosuite tutorial: http://www.evosuite.org/documentation/tutorial-part-2/
Able to generate test using the tutorial file provided by evosuite but when I tried it on one of the projects that I am working on, it couldn't generate test when I initiate mvn evosuite:generate in cmd.
understand that I first need to do a mvn compile and it actually generate all the .class files in the folder "target\classes", I actually checked that the physical files are indeed present in the folder "Classes". the folder structure is exactly the same as in the folder "src\main\java".
when I initiate "mvn evosuite:generate" ,
it actually started generating,
it is showing:
[INFO] Registered remote process from /127.0.0.1:58449
[INFO] Going to execute 139 jobs
[INFO] Estimated completion time: 278 minutes, by 2018-04-22T05:33:27.051
[INFO] Going to start job for: "Java file". Expected to end in 1158 seconds,
by 2018-04-22T01:14:45.848
and it continues until the very last java file,
showing this:
[INFO] * Updating database to "java file name"
then it came to this:
=== CTG run results ===
[INFO] Removed test suites: 0
[INFO] New test suites: 0
[INFO] Missing classes:
[INFO] "Java file name"
and ended with this statement
[INFO] WARN: failed to generate tests for 139 classes out of 139
[INFO] Stopping spawn process manager
[INFO] ---------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------
[INFO] Total time: 52:44 min
[INFO] Finished at: 2018-04-22T01:47:02+08:00
[INFO] ---------------------------------------------------------------------
I am wondering if i miss out any steps? Because using the same steps I am able to generate the test cases using the tutorial_maven provided by evosuite.
Or is this due to the complexity of the project and java files that evosuite is unable to process and generate the unit test?
Please advise! Thank you! i have tried many many different methods, mvn clean install repeatedly, but to no avail.
edit: tried eclipse plugin for evosuite as well: got the same error saying cannot find test.java file which is weird. Isn't the plugin supposed to generate the test file?

Maven Jenkins job skips all modules if one fails to build

In Jenkins, I have a Maven project with the following structure:
x proftaakmaven
- AutosimulatieSysteem
- LandenMonitoringSysteem
- PolitieSysteem
x Verplaatsingssysteem
- VerplaatsingREST
- VerplaatsingSOAP
- VerplaatsingCommon
- VerplaatsingenRabbitMQ
- RabbitMQ-Proof-of-Concept
- VerplaatsingenRabbitMQTestClient
The Maven reactor constructs this building order:
[INFO] Reactor Build Order:
[INFO]
[INFO] AutoSimulatie
[INFO] LandenMonitorSysteem
[INFO] PolitieSysteem
[INFO] VerplaatsingenSysteem
[INFO] VerplaatsingenCommon
[INFO] VerplaatsingenREST
[INFO] VerplaatsingenSOAP
[INFO] RabbitMQ-Proof-of-Concept
[INFO] VerplaatsingenRabbitMQ
[INFO] VerplaatsingenRabbitMQTestClient
[INFO] proftaakmaven
However, due to a current failure in 'LandenMonitorSysteem' source code, Maven fails on building the other modules as well. This makes the Jenkins job fail.
I have tried running Maven with --fail-never and --fail-at-end. But neither seem to have any effect.
How would I be able to continue building all the modules, even if one fails?
Thanks.
--fail-at-end should be the thing to use.
If that doesn't work you could use -pl to specify the list of working projects.
If you use the -am flag as well you can specify the target you are interested in building and Maven will calculate the dependency tree for you.
I.E. mvn clean install -pl VerplaatsingenRabbitMQTestClient -am
I have found the solution. The problem was the way that I provided the argument. In Jenkins 2.0, the job should be configured like this :
The settings inside the job

How to monitor & record build (compile/test) time?

I work on a large-ish Java project. We are using maven as our build tool, and I use Intellij (14) as my IDE.
Currently, if I build the whole project (c. 15 modules) it takes about 3 minutes. Due to the way that we use our code (even in development), I end up doing a full build quite often. On the basis that "what gets measured gets managed", I'd like to be able to monitor/record:
How many times an hour/day a build has been run?
How much time has been spent running builds?
If it were possible, to break this down by maven task - i.e. clean/compile/test/other plugins etc.
One approach might be to have the maven output dumped to files, and then have a process to read these/calculate the statistics I want. How should I approach this?
Just to be clear, I'm not asking for comments/advice on how to reduce our build-cycle runtime. We've already done a fair bit of work on this, and are continuing to work on it - indeed, I'm really looking for a way to monitor our effective progress on this.
This Maven Profile project could help you out.
Simply installing (copying) its latest version jar on your ${M2_HOME}/lib/ext and then executing the build as following
mvn clean install -Dmaven.profile
Would provide you the following sample output
com.sample:test:0.0.1-SNAPSHOT
clean 175ms
org.apache.maven.plugins:maven-clean-plugin:2.5 (default-clean) 175ms
process-resources 336ms
org.apache.maven.plugins:maven-resources-plugin:2.6 (default-resources) 335ms
compile 1s 2ms
org.apache.maven.plugins:maven-compiler-plugin:2.5.1 (default-compile) 1s 2ms
process-test-resources 9ms
org.apache.maven.plugins:maven-resources-plugin:2.6 (default-testResources) 9ms
test-compile 59ms
org.apache.maven.plugins:maven-compiler-plugin:2.5.1 (default-testCompile) 59ms
test 1s 83ms
org.apache.maven.plugins:maven-surefire-plugin:2.12.4 (default-test) 1s 83ms
package 352ms
org.apache.maven.plugins:maven-jar-plugin:2.4 (default-jar) 352ms
You would then need to have a further layer (script?) for data accumulation in order to get cross-builds statistics though. However, it already answers to your third question and it is probably a good start.

Conditionally run JUnit tests from Maven

In Maven, is there some way to specify that JUnit tests only run when there are some source code changes?
For instance, I see this message
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) # my_project ---
[INFO] Nothing to compile - all classes are up to date
Now -since no code was compiled (since there were no changes from the last compile), I'd prefer that no tests run. Is this possible?
Your project setup should be broken up into separate modules. However if you can not do that you could set the maven.test.skip property in your profiles in which you want no tests to be run. See more about skipping tests on the site.
You are talking about a war project. Typically a war project should NOT have any code in it, but rather just some configuration and stuff like html, jsp, css and java script and bundle jar projects, that contain java code. The jar projects will have the code. And if you do integration tests a good practice is to have them in separate modules.
Different war configs can be in profiles or potentially also in separate modules using the war overlay feature of the maven war plugin.

Categories