96

Familiar with Java but unfamiliar with IntelliJ, how does one "get started" with JUnit integration?

Inspired by Looking for a tutorial on using JUnit with Intellij IDEA 9.x which didn't answer my questions and was for an older version of IntelliJ.

3
  • Has the power to mark this as duplicate. But refuses to use it. Mar 4, 2018 at 7:01
  • @reversiblean what are you talking about?
    – Max
    Mar 5, 2018 at 3:21
  • Just saying this shouldn't be marked duplicate :) Mar 5, 2018 at 6:58

3 Answers 3

167
  1. Create and setup a "tests" folder
    • In the Project sidebar on the left, right-click your project and do New > Directory. Name it "test" or whatever you like.
    • Right-click the folder and choose "Mark Directory As > Test Source Root".
  2. Adding JUnit library
    • Right-click your project and choose "Open Module Settings" or hit F4. (Alternatively, File > Project Structure, Ctrl-Alt-Shift-S is probably the "right" way to do this)
    • Go to the "Libraries" group, click the little green plus (look up), and choose "From Maven...".
    • Search for "junit" -- you're looking for something like "junit:junit:4.11".
    • Check whichever boxes you want (Sources, JavaDocs) then hit OK.
    • Keep hitting OK until you're back to the code.
  3. Write your first unit test

    • Right-click on your test folder, "New > Java Class", call it whatever, e.g. MyFirstTest.
    • Write a JUnit test -- here's mine:

      import org.junit.Assert;
      import org.junit.Test;
      
      public class MyFirstTest {
          @Test
          public void firstTest() {
              Assert.assertTrue(true);
          }
      }
      
  4. Run your tests
    • Right-click on your test folder and choose "Run 'All Tests'". Presto, testo.
    • To run again, you can either hit the green "Play"-style button that appeared in the new section that popped on the bottom of your window, or you can hit the green "Play"-style button in the top bar.
5
  • Theres an easier way to do this, if i recall correctly? can you Just create a project, write a @Test and use the context sensitive error resolution to add junit as a project library. Alternatively just use ant-enter in the pom.xml to add a maven dependency. Oct 12, 2013 at 21:18
  • 3
    Hmm, maybe. I came up with this approach through trial and error, though; nothing suggested I could just write a test. This approach is a little more explicit, anyway... Also I'm not using Maven to manage my project.
    – Max
    Oct 12, 2013 at 23:06
  • 1
    thanks a lot ! Also If it's not working, or you can't find "run" you can simply restart intellij.
    – Toshi
    Feb 21, 2016 at 11:18
  • Thanks, works also for scala projects.
    – Evren
    Jul 11, 2018 at 14:37
  • Thanks man! That worked Jan 7, 2020 at 17:35
120

Basically, you only need junit.jar on the classpath - and here's a quick way to do it:

  1. Make sure you have a source folder (e.g. test) marked as a Test Root.

  2. Create a test, for example like this:

    public class MyClassTest {
        @Test
        public void testSomething() {
    
        }
    }
    
  3. Since you haven't configured junit.jar (yet), the @Test annotation will be marked as an error (red), hit f2 to navigate to it.

  4. Hit alt-enter and choose Add junit.jar to the classpath

There, you're done! Right-click on your test and choose Run 'MyClassTest' to run it and see the test results.

Maven Note: Altervatively, if you're using maven, at step 4 you can instead choose the option Add Maven Dependency..., go to the Search for artifact pane, type junit and take whichever version (e.g. 4.8 or 4.9).

1
  • Doing this results in Class not found: "Classname"Empty test suite. Jan 16, 2018 at 22:14
14

I needed to enable the JUnit plugin, after I linked my project with the jar files.

To enable the JUnit plugin, go to File->Settings, type "JUnit" in the search bar, and under "Plugins," check "JUnit.

vikingsteve's advice above will probably get the libraries linked right. Otherwise, open File->Project Structure, go to Libraries, hit the plus, and then browse to

C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 14.1.1\lib\

and add these jar files:

hamcrest-core-1.3.jar
junit-4.11.jar 
junit.jar 

Not the answer you're looking for? Browse other questions tagged or ask your own question.