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.
setUp()
annotated with @BeforeEachtearDown()
annotated with @AfterEachtestXXX()
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)} @BeforeEach public void setUp() { ${cursor} }</template><template autoinsert="true" context="java" deleted="false" description="JUnit5 AfterEach" enabled="true" name="teardown (JUnit5)">${:import(org.junit.jupiter.api.AfterEach)} @AfterEach public void tearDown() { ${cursor} }</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)} @Test public void test${name}() { ${staticImport:importStatic('org.junit.jupiter.api.Assertions.*')} ${cursor} }</template></templates>
Save this XML into a file in your preferred location. We will import this file in next step.
Import Code Templates into Eclipse
- Eclipse: Window->Preferences
- Java->Editor->Templates
- Import…
- Choose file
- Verify for template names “setup (JUnit5)”, “teardown (JUnit5)” and “test (JUnit5)” specific to JUnit 5

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.

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 !!