Creating unit test usually requires some boilerplate – you have to create separate class in proper directory, add annotations and provide test method name. While it's not much, I started to think: how can this boilerplate could be reduced?

To make our test discoverable by test framework and IDE, we usually need to annotate test method with @Test annotation.

I'm a big fan of backtick test method notation:

class RandomTestClass {
	@Test
	fun `it should do something`(){
		//
	}
}

I'm not so efficient in fast typing and "`" is quite far from "@" on keyboard. Is there a place for optimization?

IntelliJ live templates and where to find them

IDE of my choice for Kotlin and Android projects - IntelliJ Idea has already built-in live templates and completions for several languages.

On of the template I use from time-to-time is fun1 template, which simplifies creating function with just one argument:

fun1 in action

Let's see how this template is defined in IDE.

To browse live templates, first open preferences:

Then search for Live Templates in Editor section

And find fun1 in Kotlin group templates:

fun $NAME$($PARAM1$ : $PARAM1TYPE$) : $RETURN$ {
$END$
}

fun1 live template has several variables, that IDE user will be prompted to provide.

We have $NAME$ - function name, $PARAM1$ and $PARAM1TYPE$ which will be parameter name and its type, $RETURN$ - function return type and finally $END$ - anything that will go to the function body.

We have example of something very similar to what we are trying to accomplish, so let's just use copy that live template and start wondering what we would like to do:

How can we adjust it to our needs?

I'd like to have template which will give me following things:

  • test name prompt with backtick notation already present
  • @Test annotation (preferably with import)
  • prompt to write test body

Based on fun1 template I came up with test template:

@org.junit.jupiter.api.Test
fun `$NAME$`(){
    $END$
}

To create template for your JetBrains IDE, do the following steps:

  1. Add live template in Preferences:

2. Provide abbreviation (how you invoke live template), description and content of live template:

3. Define applicable context for template (in our case that will be Kotlin class)

4. Click Ok / Apply and enjoy your customized live template!

Full working example

Summary

This template has some issues:

  • it is applicable outside test scope (can be inserted in production code)
  • it uses Junit5 annotation by default (maybe I should have two templates - test4 with Junit4 annotation and test5 with Junit5 import?)

I already got used to this template. It works for me. Will it also work for you?

Even though that template is only micro-optimization and probably won't affect my productivity as much as I initially hoped, I'm glad that I finally decided to play with IntelliJ advanced features. Give custom live templates a try.

Ready to take your software skills to the next level? Schedule consultation with me.
.