How to debug maven tests remotely - java

I have a maven project. I want to debug tests that run during the project assembly. I run maven from cli
mvnDebug -DforkCount=0 test
Then I could successfully connect with remote debugger to the running jvm from Intellij. All necessary breakpoints are set. But it wouldn't stop at the breakpoints. Build successful and that is all.
This command works properly though
mvn -Dmaven.surefire.debug test
But I need to do it with the first.

I did not override plugin configuration so version 2.12 was used. The option that I needed was -DforkMode=never. Since 2.14 this option is deprecated and -DforkCount=0 should be used instead. http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

You need the debugForkedProcess option to surefire.

Related

How to skip tests when debugging test with Intellij which installs the package before debugging

I have a maven project where I'd like to debug tests using IntelliJ. The problem is that when I select the test I'd like to debug IntelliJ first executes mvn install command in order to be able to execute tests against the most recent code, and after this executes the test and allow me to debug.
I have multiple problems with this:
the project contains hundreds of tests which takes 2-3 minutes to execute them. It is super slow especially when I need to restart debugging frequently
if my test compiles, but still fails I can't start debugging since it will fail the maven install part of the execution and IntelliJ doesn't kick off debugging
I haven't found any hints in IntelliJ's doc about how to manipulate the mvn install part of the debugging process
yes, I could start maven in debug mode in the command line and attach IntelliJ to it, but that process is very fragile and it is 2021... let me use the IntelliJ fully...
I tried to add a Before launch step like mvn clean install -Dmaven.test.skip=true, but it results that the test classes won't be compiled.
My question is how can I manipulate the whole Debugging process is IntelliJ, especially the maven install part? What is the good solution here?
I'm using TestNG and the user interface doesn't offer me any option to do manipulate mvn install part.

How to make a plugin goal run regardless of test failures

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.

Maven build outside eclipse, very time consuming

My current Setup:
I have Ecliple Mars installed on Windows 10
I have some repositories checked out in my Project explorer which I basically use for any code changes, debugging purpose.
I have tomcat installed in C directory and have its path configured in the Servers section of eclipse.
So, every time I do any code changes in eclipse, I manually go into the eclipse workspace, and do the maven build using the following commandmvn -Plocal -DskipTests=true clean install, and then manually copy the war generated in the target folder into the webapps directory of my tomcat.
After doing all these, I start my tomcat in debug mode catalina jpda start and then setup Remote Java Application in eclipse for a particular module.
I can then setup breakpoints and see the changes in the debugger.
I am wondering , all of the above steps are time consuming if I have to do frequent code changes and then debug something in Debug mode in eclipse as I have to repeat steps 4 to 6 again and again for any code changes.
Could anyone tell me if there is a quick way to achieve the same goal?
You can run application dirrectly from maven via maven-{your_servlet_container}-plugin (maven-tomcat-plugin, maven-jetty-plugin, etc.). you can also specify debug mode there, so all you need to do is just run maven and connect in eclipse to remote app.
set ENV variable for always run maven in debug:
set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
(unset by setting this value to empty string)
with env variable:
mvn -Plocal -DskipTests=true clean tomcat7:run
or run mvnDebug:
mvnDebug -Plocal -DskipTests=true clean tomcat7:run
If you have your tomcat configured with eclipse properly, then you don't need to perform all these steps. I am not sure if you want to automate all these steps from outside. if yes, then either you can automate above steps with writing all in a batch file & triggering all steps with it (not a standard way) or using maven plugins to automate most of these steps.
Hope this information will help...

What is the process with Maven project to compile and test the code?

I have maven project imported in my eclipse. Now I need to start making changes to it and test it with the integration test (out of App server). Currently, the integration test is run out of server using openEJB container.
My basic question is, what is the regular process to compile, build and test with Maven?
mvn install
Maven -> Update Project.
Run my test from command line
Is it how it is done? I am specifically interested in knowing mvn install commands.
So should I do all three steps before I can test it?
Example: I just wanted to print something and see what is the output. For this I guess I need to do all these steps?
The openEJB container needs classes so it can load them.
There is a wonderful Maven quick-reference sheet at http://maven.apache.org/guides/MavenQuickReferenceCard.pdf
First, you should be aware that unit tests and integration tests are separate and are run from separate plugins and at separate parts of the maven lifecycles. Unit tests are run with surefire and integration tests are run with failsafe.
You want to run integration tests and the failsafe documentation says:
NOTE: when running integration tests, you should invoke maven with the (shorter to type too)
mvn verify
rather than trying to invoke the integration-test phase directly...
This is the best way to run integration tests directly in maven. It will run all the preceding steps necessary (eg: compile) in order to run the integration tests. It won't waste time doing an install because install happens immediately after verify.
But if you're running the tests locally, it may be a better idea to run your integration tests directly in your IDE. That will give you a much faster feedback loop.
If it is Eclipse project the most reasonable thing is to do everything not from command line but from Eclipse. Assuming you have m2e plugin installed, go to your_project->run as->Maven test and run it.
You dont need neither install nor package phase to run Maven tests, package will create a jar which is not needed for tests, install will copy this jar to local repo which is also useless. When Maven run tests it uses compiled classes from target dir and ignores project's jar if even it exists.
Yes, mvn isntall is the most popular option. It compiles, packages and tests your project.

Maven build debug in Eclipse

I want to debug Eclipse build with tests. I tried to run it by Run > Debug Configurations > Maven Build. In Base directory is my Maven repo directory with pom.xml file, in goals 'clean install'. When I click on debug Eclipse starts build, run tests but it doesn't stops on breakpoints.
Easiest way I find is to:
Right click project
Debug as -> Maven build ...
In the goals field put -Dmaven.surefire.debug test
In the parameters put a new parameter called forkCount with a value
of 0 (previously was forkMode=never but it is deprecated and doesn't work anymore)
Set your breakpoints down and run this configuration and it should hit the breakpoint.
if you are using Maven 2.0.8+, then it will be very simple,
run mvndebug from the console, and connect to it via Remote Debug Java Application with port 8000.
probleme : unit test result are not the same runing with eclipse and maven due ti order of library used by eclipse and maven.
In my case the test was success with maven but i want to debug my unit test using eclipse, so
the most easy way to debug unit test class with eclipse and runing maven is :
1) mvn -Dtest=MySuperClassTest -Dmaven.surefire.debug test ==> it will listen to the 5005 port (default port)
2) Go to eclipse, open a debug configuration, add a new java remote application and change the port to 5005 and debug
3) of course you must add break point somewhere in the class that you want to debug
The Run/Debug configuration you're using is meant to let you run Maven on your workspace as if from the command line without leaving Eclipse.
Assuming your tests are JUnit based you should be able to debug them by choosing a source folder containing tests with the right button and choose Debug as... -> JUnit tests.

Categories