151

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,

Scruffers
  • 4,984
  • 8
  • 35
  • 41
  • The directories structures for tests and tested classes must fit. Here is how to do it easily: http://stackoverflow.com/a/36057708/715269 – Gangnus Mar 18 '16 at 08:20

9 Answers9

274

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.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Jarl
  • 3,120
  • 1
  • 18
  • 8
  • 1
    In my project for some reason, even though the class and the test class are located in the same package, when I did Ctrl+Shift+T it says "No Test Class Found". Also, when I right click on the test file, it only gives me the option to compile test, but not run test. Any idea? – Glide Nov 17 '11 at 01:34
  • 1
    Off hand I'd answer as Andreas did at your [other question](http://stackoverflow.com/questions/8161058/intellij-mvn-project-test-classes-not-functioning). Somehow it doesn't recognize that your classes are tests. – Jarl Nov 17 '11 at 23:22
  • Still works nicely with 12.1.4 in Windows. Check in Settings that the Keymap binds Mainmenu/Navigate/Test to Ctrl+Shift+T – Jarl Aug 22 '13 at 11:37
  • 4
    Didn't work for me - _until_ I re-enabled the JUnit plugin which had somehow been disabled. – Rachel K. Westmacott Jan 14 '14 at 15:26
  • Works in 13.0.2 on Windows 7. – Bulwersator Feb 27 '14 at 07:06
  • Does not work w my IntelliJ IDEA 14 Win: I get error "No test subjects found" if I ctrl-shift-t on class or no response at all if using menu or keys on project tree. JUnit is enabled. I was able to get the Create Test popup if I clicked into class body and then used menu or ctrl-shift-t. – davidjmcclelland Nov 20 '14 at 21:40
  • 4
    It should be noted that *Navigate > Test* is disabled when the class is selected in the Project window. It must be selected in an editor tab. – Kevin Krumwiede Dec 22 '15 at 17:42
  • in the `Create Test` Dialog, how can I check all the methods at once? – ycomp Feb 10 '16 at 23:16
  • In IntelliJ 2020.1.2, right-click on the class name in the editor. Then Generate -> Test. – alex Jul 08 '20 at 01:47
45

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.

vahid abdi
  • 9,636
  • 4
  • 29
  • 35
rmoestl
  • 3,059
  • 5
  • 24
  • 38
12

*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.

vahid abdi
  • 9,636
  • 4
  • 29
  • 35
user1412523
  • 198
  • 1
  • 6
9

Use the menu selection Navigate > Test

gif

Shortcuts:

Windows

Ctrl + Shift + T

macOS

+ Shift + T

Abraham
  • 8,525
  • 5
  • 47
  • 53
4

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

Hamish
  • 1,015
  • 9
  • 20
4
  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!

Jugal Panchal
  • 1,448
  • 16
  • 20
  • 1
    Thank you, I was getting "Generate tests in same root as source?" trying the above, this was just what I needed. – jgreve Feb 22 '20 at 03:36
2

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.

Jacky
  • 8,619
  • 7
  • 36
  • 40
0

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.

Da_Vicki
  • 91
  • 1
  • 8
0

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'.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60