Configuring IntelliJ IDEA for unit testing with JUnit

Unit TestingJunitIntellij Idea

Unit Testing Problem Overview


I decided to try out IntelliJ this morning via the trial version and installed the JUnit plugin. I made a new Java project and I want to write a test case for it.

How do I add the junit.jar file to my project? (I actually want to add it to every java project, now and forever more - is there a way of doing that?).

Unit Testing Solutions


Solution 1 - Unit Testing

If you already have a test class, but missing the JUnit library dependency, please refer to Configuring Libraries for Unit Testing documentation section. Pressing Alt+Enter on the red code should give you an intention action to add the missing jar.

However, IDEA offers much more. If you don't have a test class yet and want to create one for any of the source classes, see instructions below.

You can use the Create Test intention action by pressing Alt+Enter while standing on the name of your class inside the editor or by using Ctrl+Shift+T keyboard shortcut.

A dialog appears where you select what testing framework to use and press Fix button for the first time to add the required library jars to the module dependencies. You can also select methods to create the test stubs for.

Create Test Intention

Create Test Dialog

You can find more details in the Testing help section of the on-line documentation.

Solution 2 - Unit Testing

Press Ctrl+Shift+T in the code editor. It will show you popup with suggestion to create a test.

Mac OS: āŒ˜ Cmd+Shift+T

Solution 3 - Unit Testing

One way of doing this is to do add junit.jar to your $CLASSPATH as an external dependency.

adding junit intellij

So to do that, go to project structure, and then add JUnit as one of the libraries as shown in the gif.

In the 'Choose Modules' prompt choose only the modules that you'd need JUnit for.

Solution 4 - Unit Testing

If you already have test classes you may:

  1. Put a cursor on a class declaration and press Alt + Enter. In the dialogue choose JUnit and press Fix. This is a standard way to create test classes in IntelliJ.

  2. Alternatively you may add JUnit jars manually (download from site or take from IntelliJ files).

Solution 5 - Unit Testing

In my case (IntelliJ 2020-02, Kotlin dev) JUnit library was already included by Create project wizard. I needed to enable JUnit plugin:

IntelliJ JUnit plugin

to get green Run test icons next to each test class and method:

enter image description here

and CTRL+Shift+R will run test under caret, and CTRL+shift+D to debug.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBobView Question on Stackoverflow
Solution 1 - Unit TestingCrazyCoderView Answer on Stackoverflow
Solution 2 - Unit TestingIgor KonoplyankoView Answer on Stackoverflow
Solution 3 - Unit TestingGayan WeerakuttiView Answer on Stackoverflow
Solution 4 - Unit TestingirudyakView Answer on Stackoverflow
Solution 5 - Unit TestingRobert LujoView Answer on Stackoverflow