Is there a way to run a JUnit test for a particular class in IntelliJ without compiling the entire project?
Asked Answered
C

3

9

I have a project in IntelliJ that has a number of syntactically correct and incorrect classes and a unit test that I've written for a particular class.

Is there a way to execute the test even though other classes, completely independent from the current class I want to test, have errors that prevent the entire project from compiling?

The class I want to test has no compile-time errors. When I try to run the test, I'm directed to those other classes with syntax errors.

Climax answered 28/4, 2017 at 12:37 Comment(0)
C
8

Replace the Before launch Build step with Build, no error check:

no error check

Do this change for the default JUnit configuration (under Defaults node in the tree on the left) so that it's applied to all the new JUnit run/debug configurations automatically.

You can also remove the Build step and compile the test files manually (from the file right click context menu):

recompile

Complaisant answered 28/4, 2017 at 13:11 Comment(1)
Doesn't work well with latest version of IntelliJ (here 2023.2.2). I get TestEngine with ID 'junit-jupiter' failed to discover tests.Chivers
F
1

As of 11 June 2024, skipping the build is now under "Modify Options" in Run/Debug Configuration.

Tick the "Do not build before run" option.

enter image description here

Floccus answered 11/6 at 9:46 Comment(1)
This configuration does not take into account the changes, made during e.g. fixing some bug. Without build, changes will not be applied in the debug mode.Micropyle
G
0

From a quick glance, it does seem like the answer of Paolo is the only current day option, i.e. going to Modify Run Configuration and checking Do not build before run.

You would then have to build manually, if you make changes to the file, which works as follows. Run the Recompile <filename> action (for example, double shift and search for the action). Then proceed to run the test with build turned off. For me, this also has the shortcut CTRL+Shift+F9.

If this action is not available to you: Turn off the "Do not build before run" option again, attempt to build, and now IntelliJ should recognize the file again. You may then execute the Recompile action again.

Be aware that modifying run configurations is dangerous, as you might return back to the test after months, modify it, and spend a long time making changes until you realize that the file was never actually compiled.

Gotha answered 16/9 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.