autofunk
autofunk

Reputation: 63

Unable to execute unit tests in a maven project from within intellij

I have inherited a legacy project with 100s of tests, and dependencies defined within the pom.

All of the tests run when I execute a mvn clean install from the command line, but when I try to execute one of these tests in debug mode from within Intellij i get the following error.

java.lang.NoClassDefFoundError: Could not initialize class

How can I get intellij to recognise these dependencies when trying to run a test in debug mode from the ide?

Upvotes: 2

Views: 3044

Answers (4)

Narrim
Narrim

Reputation: 570

You can run tests using the maven project window in IntelliJ View - Tool Windows - Maven Projects

Then under the project or module you wish to test open the lifecycle goals and click test.

This will run the currently configured test goal. Now the report is logged into the target directory

I use https://github.com/destin/maven-test-support-plugin to view the test results.

You can access this screen once the plugin is installed from the Project window again right click on the projects root and select "Show Test Results" (should be below the maven icon)

Good luck

Upvotes: 0

Shl.Xue
Shl.Xue

Reputation: 1

IntelliJ execute unit tests without mvn command.

a IntelliJ plugin: MvnRunner

Upvotes: 0

autofunk
autofunk

Reputation: 63

I managed to solve this by simply changing the working directory location in the Run configuration to point to the correct classpath location. The default location had been taken from a parent project.

Upvotes: 2

Ashutosh Jindal
Ashutosh Jindal

Reputation: 18869

It would be very convenient if one was able to debug tests from within IntelliJ. I have run into similar issues with my massive project and have found a workaround for it.

When wishing to debug a test, I have often found it useful to use a remote debugging session like so:

 mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE"  -Dtest=com.autofunk.TheFunkImplTest test -DfailIfNoTests=false

You can then attach to the above using a remote debug session from within IntelliJ on port 8000.

Therefore, when debugging tests I first check to see whether a simple debug from within Intellij works. If not, I run the above and then attach to it using a debug configuration like so:

enter image description here

Upvotes: 0

Related Questions