Thomas Sundberg

June 1, 2012

Adding a JUnit test template in IntelliJ IDEA

Filed under: Automation, Java, Programming — Tags: , , , — Thomas Sundberg @ 07:33

This post has been migrated to

A test template for JUnit tests doesn’t exist out of the box for IntelliJ IDEA. Creating one is not complicated.

The goal is to create a simple template that will assist you when you need to write a test in JUnit. I want it to look something like this:

public void should_assert_a_behaviour() {
    // Given
    // Setup the system under test

    // When
    // Execute the system under test

    // Then
    // Assert that the expected change has occurred

To add it follow, these steps:

  • Open ‘Preferences’ or ‘Settings’ (depending on your os)
  • Open ‘Live Templates’
  • Select the group you want to store your template in. I will store it next to the template for a main, ‘other’
  • Click on the ‘+’
  • Fill out the form at the bottom of the page
    • Abbreviation: ‘test’
    • Description: ‘A test template’
    • Fill in the template test as above
    • Define the context, expand ‘Java’, select ‘Declaration’
  • Click ‘Ok’


To use the new template, just press cmd j or ctrl j (depending on your os) and select ‘test’.



  1. Actually, IntelliJ has a template for creating JUnit tests, but it is available as a “File template” and not a “Live template”:

    1. Open your test class
    2. “Right click” and select “Generate…” (or simply press “ctrl” + “n” if you use mac)
    3. Select “Test Method” and implement your test

    To change the template:

    1. Open “Settings”
    2. Select “File Templates”
    3. Select “Code”
    4. Select “JUnit4 Test Method” and do your changes

    Comment by Mattias Severson — June 6, 2012 @ 18:24

    • This was news to me. I will make I try it out to see if does what I want it to do.

      Thanks for telling!

      Comment by Thomas Sundberg — June 7, 2012 @ 19:00

  2. And also, if you are looking at the class you want to test, just press Ctrl-Shift-T (or Cmd-Shift-T) and it will create the test class for you after asking whether you want to use junit 3,4 or testng.

    Comment by Jamie Briant — November 27, 2013 @ 06:25

    • The only issue I have with that is that I must create the class I want to test before I start writing the tests. I prefer to work the other way around, create the test and then see which classes I actually have to implement.


      Comment by Thomas Sundberg — November 27, 2013 @ 07:24

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: