This article is from MDC Advent Calendar 2020 Day 19.
Do you guys write unit tests? (I myself haven't written much recently ...)
--I can implement it, but I can't write much test code --I've written up to JUnit4, but JUnit5 doesn't have much ...
We hope this will help you get started writing test code in JUnit 5 for those who are surprisingly many.
—— Needless to say, it's the most major testing framework in Java development. --Unlike previous versions of JUnit, JUnit 5 consists of multiple modules contained in three subprojects. JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
TestEngine
API for developing test frameworks that run on the above platforms
--Provides Console Launcher
to launch the platform from the command line, build plugins for Gradle and Maven, JUnit4-based test runners, etc. so that you can run any TestEngine
.TestEngine
to run JUnit3 or JUnit4-based tests――It's been a long time, but if you want to start writing tests with JUnit 5 for the time being, you can add ** JUnit Jupiter ** to the dependency (the following is a sample of gradle)
build.gradle
plugins {
id "java"
}
sourceCompatibility = 8
targetCompatibility = 8
[compileJava, compileTestJava]*.options*.encoding = "UTF-8"
repositories {
mavenCentral()
}
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:5.7.0"
}
--Java 8 or above is required to run JUnit5
package sample.junit5;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JUnit5Test {
@Test
void success() {
assertEquals(12, 12);
}
@Test
void failure() {
assertEquals(5, 12);
}
}
Annotation | Explanation |
---|---|
@Test |
The given method becomes a test method |
--Use the assertion method (eg assertEquals (expected, actual)
) to compare the expected and measured values
--Actually, call the method under test and compare its return value with the expected value.
--In JUnit5, both test classes and test methods no longer need to be public
.
package sample.junit5;
import org.junit.jupiter.api.*;
class JUnit5Test {
@BeforeAll
static void beforeAll() {
System.out.println("☆ beforeAll()");
}
@BeforeEach
void beforeEach() {
System.out.println("☆☆ beforeEach()");
}
@AfterEach
void afterEach() {
System.out.println("★★ afterEach()");
}
@AfterAll
static void afterAll() {
System.out.println("★ afterAll()");
}
@Test
void test1() {
System.out.println("~~~ test1() ~~~");
}
@Test
void test2() {
System.out.println("~~~ test2() ~~~");
}
}
Execution result
☆ beforeAll()
☆☆ beforeEach()
~~~ test1() ~~~
★★ afterEach()
☆☆ beforeEach()
~~~ test2() ~~~
★★ afterEach()
★ afterAll()
Annotation | Explanation |
---|---|
@BeforeAll |
The given method is executed only once at the very beginning Method must be static |
@BeforeEach |
The given method is executed every time before each test method |
@AfterAll |
The given method is executed only once at the very end Method must be static |
@AfterEach |
The given method is executed after each test method |
package sample.junit5;
import org.junit.jupiter.api.*;
class JUnit5Test {
@Test
void test1() {...}
@Nested
class group1 {
@Test
void test2() {...}
@Test
void test3() {...}
}
}
Annotation | Explanation |
---|---|
@Nested |
Test classes can be nested by giving them to non-static classes |
――The test class often has a certain number of steps, so it is recommended to group by test viewpoint.
package sample.junit5;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class JUnit5Test {
@ParameterizedTest
@ValueSource(strings = {"foo", "bar", "baz"})
void test(String value) {
System.out.println("VALUE: " + value);
}
}
Execution result
VALUE: foo
VALUE: bar
VALUE: baz
Annotation | Explanation |
---|---|
@ParameterizedTest |
The given method becomes a parameter test |
@ValueSource |
You can specify one array of literal values and provide one parameter in the parameter test call (eg String) |
--It is an image that the test is executed for the number of parameters --The following literal values are provided
short
byte
int
long
float
double
char
java.lang.String
java.lang.Class
――I'm sorry, I haven't had enough time to write half of what I want to write (... I started writing after 23:00 on 12/18). --Anyway, JUnit 5 is convenient and easy --Everyone, let's write the test code properly
Recommended Posts