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.
assumption
| Language | Version |
|---|---|
| Java | 8 or higher |
Class annotation changes
The @ExtendWith(SpringExtension.class) annotation is used to annotate the test class from JUnit5.
@ExtendWith(SpringExtension.class)
public class{
public Some_kind_of_test() {
// test
}
}
@SpringJUnitConfig(classes = AppConfig.class) is the same as below.
↓
@ExtendWith(SpringExtension.class) @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.
@ExtendWith(SpringExtension.class) @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.
AppConfig.java
package com.example.demo.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example.demo.primary.repository")
public class AppConfig {}
No need for public modifier
@ExtendWith(SpringExtension.class)
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
srt/test/resources/junit-platform.properties
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;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@DataJpaTest
class LinkedserviceMansionServiceMstRepositoryTest {
@BeforeAll
static void initAll() {}
@BeforeEach
void beforeEach() {}
@AfterEach
void tearDown() {}
@AfterAll
static void tearDownAll() {}
@Test
@Order(1)
@DisplayName("test case-1")
void successTest() {
// ~
}
@Test
@Order(2)
@DisplayName("test case-2")
void failureTest() {
// ~
}
}
Annotations Obsolete in JUnit5
The following has been discontinued
| Annotations Obsolete in JUnit5 |
|---|
| @RunWith(Enclosed.class) |
| @RunWith(Theories.class) |
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.



コメント