Features changed or added in JUnit5

Features changed or added in JUnit5

It’s been about 10 years since JUnit 4 was a major version upgrade.

We looked at the differences.


Language Version
Java 8 or higher

Class annotation changes

The @ExtendWith(SpringExtension.class) annotation is used to annotate the test class from JUnit5.

public class{
  public Some_kind_of_test() {
    // test

@SpringJUnitConfig(classes = AppConfig.class) is the same as below.

@ContextConfiguration(classes = AppConfig.class)

DI with TCF function

By adding the following annotation to the test class, it is possible to DI to classes that have dependencies.

@ContextConfiguration(classes = AppConfig.class)

Place AppConfig.java somewhere in src/main/java.

The class name does not have to be AppConfig. Here it is named AppConfig.java.


package com.example.demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

public class AppConfig {}

No need for public modifier

public class AaaTest{
  public 何かのテスト() {
    // テスト

In JUnit 4, the above was written as above, but both classes and methods no longer need the public modifier.

class AaaTest{
  何かのテスト() {
    // テスト

Can be tested in parallel

You can run tests in parallel from JUnit5. This speeds up test execution dramatically. This is a very powerful feature.

Place the junit-platform.properties file in the classpath root, with the contents as follows


junit.jupiter.execution.parallel.enabled=true # Enable Concurrency
junit.jupiter.execution.parallel.mode.default=concurrent # Simultaneous defaults (I don't think it's necessary)

Specifically, place junit-platform.properties under src/test/resources.

Specify test execution order with @TestMethodOrder annotation

Attach @TestMethodOrder(MethodOrderer.OrderAnnotation.class) to the class to specify the order in which each @Test is executed.

Attach @Order(org.junit.jupiter.api.Test) annotation to each test method.

Specify a numerical value for the argument and the tests will be executed in the specified order.

package jp.co.confrage;

class LinkedserviceMansionServiceMstRepositoryTest {
  static void initAll() {}

  void beforeEach() {}

  void tearDown() {}

  static void tearDownAll() {}

  @DisplayName("test case-1")
  void successTest() {
    // ~

  @DisplayName("test case-2")
  void failureTest() {
    // ~

Annotations Obsolete in JUnit5

The following has been discontinued

Annotations Obsolete in JUnit5

Pretreatment / Post-treatment

We do pre/post processing before testing, but the annotations have changed from JUnit 4.

Processing Meaning mocha
@BeforeAll Static method to be executed only once before testing @Before
@BeforeEach Executed only once as the initial process for each test method @BeforeEach
@AfterAll Static method to be executed only once after the test @After
@AfterEach Executed only once as the termination process for each test method @AfterEach

Skip test

In JUnit 5, you can use the org.junit.jupiter.api.Disabled annotation to skip tests at the class level or at the method level.

Simply grant the @Disabled annotation.


Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL