Create Test Class in IntelliJ

JavaUnit TestingIntellij Idea

Java Problem Overview


I'm working within the traditional Maven Java project structure inside IntelliJ, e.g.

main/java/com/x/y
test/java/com/x/y

When I create a class called Foo in main/java/com/x/y using IntelliJ I would like to automatically have a file called FooTest.java created in test/java/com/x/y.

In Eclipse there is a button on the toolbar that will do much of the work involved in creating a skeleton test class, does anyone know of something similar in IntelliJ?

Thanks,

Java Solutions


Solution 1 - Java

Use the menu selection Navigate -> Test, or Ctrl+Shift+T (Shift++T on Mac). This will go to the existing test class, or offer to generate it for you through a little wizard.

Solution 2 - Java

Alternatively you could also position the cursor onto the class name and press alt+enter (Show intention actions and quick fixes). It will suggest to Create Test.

At least works in IDEA version 12.

Solution 3 - Java

**IntelliJ 13 ** (its paid for) We found you have to have the cursor in the actual class before ctrl+Shift+T worked.

Which seems a bit restrictive if its the only way to generate a test class. Although in retrospect it would force developers to create a test class when they write a functional class.

Solution 4 - Java

Use the menu selection Navigate > Test

gif

Shortcuts:

Windows

Ctrl + Shift + T

macOS

+ Shift + T

Solution 5 - Java

I can see some people have asked, so on OSX you can still go to navigate->test or use cmd+shift+T

Remember you have to be focused in the class for this to work

Solution 6 - Java

  1. Right click on project then select new->directory. Create a new directory and name it "test".
  2. Right click on "test" folder then select Mark Directory As->Test Sources Root
  3. Click on Navigate->Test->Create New Test
    Select Testing library(JUnit4 or any)
    Specify Class Name
    Select Member

That's it. We can modify the directory structure as per our need. Good luck!

Solution 7 - Java

I think you can always try the Ctrl + Shift + A to find the action/command you need.
Here you can try to press Ctrl + Shift + A and input «test» to find the command.

Solution 8 - Java

Use @Test annotation on one of the test methods or annotate your test class with @RunWith(JMockit.class) if using jmock. Intellij should identify that as test class & enable navigation. Also make sure junit plugin is enabled.

Solution 9 - Java

With the cursor on the class name declaration I do ALT + Return and my Intellij 14.1.4 offers me a popup with the option to 'Create Test'.

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
QuestionScruffersView Question on Stackoverflow
Solution 1 - JavaJarlView Answer on Stackoverflow
Solution 2 - JavarmoestlView Answer on Stackoverflow
Solution 3 - Javauser1412523View Answer on Stackoverflow
Solution 4 - JavaabranheView Answer on Stackoverflow
Solution 5 - JavaHamishView Answer on Stackoverflow
Solution 6 - JavaJugal PanchalView Answer on Stackoverflow
Solution 7 - JavaJackyView Answer on Stackoverflow
Solution 8 - JavaDa_VickiView Answer on Stackoverflow
Solution 9 - JavaAdriaan KosterView Answer on Stackoverflow