JUnit 5 Test Templates for Eclipse

Eclipse has great tooling support for JUnit test cases. Having code templates for JUnit test cases configured in Eclipse is a great addition in faster test development. Learn to create and import JUnit 5 test templates in eclipse.

JUnit 5 Test Templates

Given below template file configure three JUnit 5 template methods i.e.

  1. setUp() annotated with @BeforeEach
  2. tearDown() annotated with @AfterEach
  3. testXXX() annotated with @Test

When you use test() template, it automatically adds this import statement:

import static org.junit.jupiter.api.Assertions.*;

It enable to use all static assertXXX() methods directly into test.

<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java" deleted="false" description="JUnit5 BeforeEach" enabled="true" name="setup (JUnit5)">${:import(org.junit.jupiter.api.BeforeEach)}
public void setUp() {
}</template><template autoinsert="true" context="java" deleted="false" description="JUnit5 AfterEach" enabled="true" name="teardown (JUnit5)">${:import(org.junit.jupiter.api.AfterEach)}
public void tearDown() {
}</template><template autoinsert="false" context="java-members" deleted="false" description="JUnit5 test method" enabled="true" id="org.eclipse.jdt.ui.templates.test" name="test (JUnit5)">${:import(org.junit.jupiter.api.Test)}
public void test${name}() {

Save this XML into a file in your preferred location. We will import this file in next step.

Import Code Templates into Eclipse

  1. Eclipse: Window->Preferences
  2. Java->Editor->Templates
  3. Import…
  4. Choose file
  5. Verify for template names “setup (JUnit5)”, “teardown (JUnit5)” and “test (JUnit5)” specific to JUnit 5
JUnit 5 Test Templates for Eclipse
JUnit 5 Test Templates for Eclipse

How to Use Test Templates

To use template, place the cursor in new line and type 2-3 initials of template method. Now hit CTRL+ENTER. It will open a pop-up in below manner.

Use JUnit 5 Test Templates in Eclipse
Use JUnit 5 Test Templates in Eclipse

Choose the template method with arrow up or down keys and hit ENTER.

It will generate the template code where your cursor was placed

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.