20

Is there a short cut to create a test method quickly in IntelliJ? What I want to happen is if I already have a test class called TestClass, then in my Class file if I create a new method I could right click on the new method and click something like 'create test' on the method itself. There is an option available for the class but I can't see it for the method level. Take a look at this photo and you'll see that the option is available for the class(by pressing alt + enter), but I'd like it available for the method also.

on the class level

UPDATE: It seems the option might be in IntelliJ but I cannot find this in Android Studio 1.1. When I hit alt+enter on a method in android studio (where the hosting class already has a test case) I get this menu:

enter image description here

and here is the funny thing in android studio, if I go into the test case class itself then I see an option to create test method within that class. It's just not showing up in the concrete class I need to be tested:

enter image description here

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • What IntelliJ version are you using? When I hit ALT+ENTER on method name I get option _Generate missed test methods_. When I select it, it shows a list of all methods which do not have unit tests, with only the one you hit ALT+ENTER on selected by default. After you hit ENTER a test method is then created in the existing JUnit test class for it. I'm using latest IntelliJ IDEA btw. – Bohuslav Burghardt Feb 15 '15 at 12:35
  • Can you define "latest", please. 14.0.3 ? – vikingsteve Feb 15 '15 at 12:54
  • @vikingsteve Yes 14.0.3 (latest stable release) – Bohuslav Burghardt Feb 15 '15 at 13:08
  • Method has to be public to have this option for test generation. I had the same problem for protected methods. – LadyWoodi Apr 19 '18 at 13:36

5 Answers5

49

Android Studio Ctrl+Shift+T or Navigate->Test.

mx0
  • 6,445
  • 12
  • 49
  • 54
Honghe.Wu
  • 5,899
  • 6
  • 36
  • 47
14

For MacOS, it is:

Command (or Cmd) ⌘ + Shift + T

Reid Spencer
  • 2,776
  • 28
  • 37
Vivek
  • 141
  • 1
  • 4
10

Here is one way to achieve this (tested in IntelliJ 14.0.3):

  • Navigate to the method you wish to create a unit test for
  • Hit ALT+ENTER
  • Select Generate Missed Test Methods

After that dialog will be shown, containing all methods in a given class with only the one you invoked the Generate Missed Test Methods action on selected by default. After you hit OK in the dialog a new test method for selected methods will be added to the existing unit test class.

Here is also a screenshot of the dialog:

enter image description here

Unfortunately, I haven't found the corresponding action in the keymap settings, so this probably can't be bound directly to some custom keyboard shortcut and it's only accessible via the ALT-ENTER menu.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Bohuslav Burghardt
  • 33,626
  • 7
  • 114
  • 109
0

None of these answers worked for me. I had to actually click the item in the menu (the keyboard shortcut opened something else)

enter image description here

MSpeed
  • 8,153
  • 7
  • 49
  • 61
  • @Honghe.Wu answered the same: Ctrl+Shift+T (Cmd + Shift + T) or Navigate->Test. – CoolMind Nov 19 '20 at 11:43
  • As per my comment above, the keyboard shortcut didn't work (not sure why - this was a very long time ago!) – MSpeed Nov 19 '20 at 12:01
  • Well, sometimes it happens. For instance, Ctrl + Shift + M doesn't work, see https://stackoverflow.com/questions/45665905/android-studio-extract-method-shortcut-ctrlaltm-not-working/51493800. – CoolMind Nov 19 '20 at 12:13
0

If you want only create a method, the shortcut is:

  1. Command (or Cmd) ⌘ + N
  2. Select Test function
  3. Input the perfect name for your test

enter image description here

Yani Morales
  • 129
  • 5