...
Info |
---|
The tutorial repository can be found here: https://gitlab.eufus.eu/bpogodzinski/ach-tutorials/-/tree/TDD-java/TDD-java/Overview Test class is in |
A basic test class looks like this:
Code Block |
---|
import org.junit.jupiter.api.DisplayName;
@DisplayName("JUnit 5 Example")
class JUnit5ExampleTest {
.....
} |
Setup and Teardown methods
A test class can have four setup and teardown methods that must fulfill these two conditions:
- These methods must not return anything. In other words, their return type must be
void
. - Setup and teardown methods cannot be
private
.
The supported setup and teardown methods are described in the following:
- The method that is annotated with the
@BeforeAll
annotation must bestatic
, and it's run once before any test method is run. - The method that is annotated with the
@BeforeEach
is invoked before each test method. - The method that is annotated with the
@AfterEach
annotation is invoked after each test method. - The method that is annotated with the
@AfterAll
annotation must bestatic
, and it's run once after all test methods have been run.
Let's add these methods to our test class. After we have added these setup and teardown methods to our test class, its source looks as follows:
Code Block |
---|
import org.junit.jupiter.api.*;
@DisplayName("JUnit 5 Example")
class JUnit5ExampleTest {
@BeforeAll
static void beforeAll() {
System.out.println("Before all test methods");
}
@BeforeEach
void beforeEach() {
System.out.println("Before each test method");
}
@AfterEach
void afterEach() {
System.out.println("After each test method");
}
@AfterAll
static void afterAll() {
System.out.println("After all test methods");
}
} |
After we have added setup and teardown methods to our test class, we can finally write our first test methods. Let's find out how we can do it.
Writing Our First Test Methods
A test method is a method that fulfills these three requirements:
- A test method isn't
private
orstatic
- A test method must not return anything. In other words, its return type must be
void
. - A test method must be annotated with the
@Test
annotation.
Let's add two test methods to our test class:
- The
firstTest()
method has a custom display name and it writes a unique string toSystem.out
. - The
secondTest()
method has a custom display name and it writes a unique string toSystem.out
.
After we have written these two test methods, the source code of our test class looks as follows:
Code Block |
---|
import org.junit.jupiter.api.*;
@DisplayName("JUnit 5 Example")
class JUnit5ExampleTest {
@BeforeAll
static void beforeAll() {
System.out.println("Before all test methods");
}
@BeforeEach
void beforeEach() {
System.out.println("Before each test method");
}
@AfterEach
void afterEach() {
System.out.println("After each test method");
}
@AfterAll
static void afterAll() {
System.out.println("After all test methods");
}
// Added test methods
@Test
@DisplayName("First test")
void firstTest() {
System.out.println("First test method");
}
@Test
@DisplayName("Second test")
void secondTest() {
System.out.println("Second test method");
}
} |
We have just written our first test methods. Let's see what happens when we run our unit tests.
Code Block |
---|
Before all test methods
Before each test method
First test method
After each test method
Before each test method
Second test method
After each test method
After all test methods
|
This output proves that the setup, teardown, and test methods are run in the correct order. Let's summarize what we learned from this blog post.
Summary
This tutorial has taught us four things:
- A test class is a normal Java class that is either
public
or package private. - Setup and teardown methods must not be
private
and they must not return anything. - A test method is a method that isn't
private
and doesn't return anything. - We could specify the display name of a test class and a test method because this allows us to replace technical names with sentences that make sense.