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:
@Test
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'.
Resources
- IntelliJ IDEA - still the best Java editor available
- Thomas Sundberg - The author