Technical blog from Craig Russell.


Defining a Live Template for Unit Tests

This post will describe how to define a live template in Intellij/Android Studio which can be used to quickly add a unit test in the exact style you prefer.

When I add a new unit test, I want it to be as quick as possible but I also need to adhere to the team’s style on unit test naming. I want to define my when/then conditions, and have the cursor ready to type with as little effort as possible.

Creating the Live Template

Create a live template, from the Preferences menu.

Editor -> Live Templates

defining the live template

Create new template, and give it an abbreviation. I’ve used test in this example. This is the word you need to type, followed by ENTER key, to invoke the live template.

fun when$WHEN$Then$THEN$() {

Adapt the template body to suit your team’s unit test naming conventions. Note that @org.junit.Test will automatically resolve to simply @Test so long as Shorten FQ names is checked. This will also add the appropriate import statement for you if it isn’t already added.

For applicable contexts, I’ve chosen Kotlin Classes.

Template in Action

  1. I type out the word test and hit ENTER key. It automatically populates the surrounding boilerplate and sets my cursor ready to type the when condition for my unit test name.
  2. Hitting ENTER again sets the cursor ready to type the then condition.
  3. A final ENTER, and the cursor is in the body of the test ready to go.