Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up JUnit with IntelliJ IDEA [duplicate]

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.

like image 896
Max Avatar asked Oct 12 '13 05:10

Max


People also ask

How do I create a JUnit test case automatically in Intellij?

Add a new testIn your production code in the editor, place the caret at the class for which you want to create a test, press Alt+Enter , and select Create Test. In the Create Test dialog, select the library that you want to use. If you don't have the necessary library yet, you will be prompted to download it.


1 Answers

  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.
like image 55
Max Avatar answered Oct 14 '22 10:10

Max