Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

This page describes how we can write our first test class with JUnit 5. After we have finished this blog postpage, we:

  • Can create test classes with JUnit 5.
  • Know how we can use setup and teardown methods.
  • Understand how we can write simple test methods with JUnit 5.

...

Info

The tutorial repository can be found here:

https://gitlab.eufus.eupsnc.pl/bpogodzinskiach/ach-tutorials/-/tree/TDD-java/TDD-java/Overview

Test class is in src/test/java/JUnit5OverviewTest.java 

A basic test class looks like this:

Code Block
import org.junit.jupiter.api.*;

@DisplayName("JUnit 5 Overview class")
class JUnit5OverviewTest {

    @Test
    void exampleTest() {
        System.out.println("example test method");
    }

}

Setup  Setup and Teardown methods

A test class can have four setup and teardown methods that must fulfill these two conditions:

...

  • The method that is annotated with the @BeforeAll annotation must be static, 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 be static, and it's run once after all test methods have been run.

...

Code Block
import org.junit.jupiter.api.*;
 
@DisplayName("JUnit 5 Example")
class JUnit5ExampleTest {
 
    @BeforeAll
    static void beforeAll() {
        System.out.println("Before all test methods");
//        Some examples of common expensive operations are:
//       - the creation of a database connection
//       - the startup of a server.     }
 
    @BeforeEach
    void beforeEach() {
        System.out.println("Before each test method");
//        This is useful when we want to execute some common code before running a test.
//        - list initialization
     }
 
    @AfterEach
    void afterEach() {
        System.out.println("After each test method");
    }
 
    @AfterAll
    static void afterAll() {
        System.out.println("After all test methods");
    }
}

@DisplayName and @Disabled

Now let's move to new test-optional methods:

...

}

...

...


As we can see, we can change the display name or disable the method with a comment, using these annotations.

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:

...

...


Let's add two test methods to our test class:

The firstTest() method

...

and

...

 

...

secondTest() method

...

have a custom display name and

...

they write a unique string to System.out.After we have written these two test methods, the source


Code Block

...


Code Block
import org.junit.jupiter.api.*;
 
@DisplayName("JUnit 5 ExampleOverview class")
class JUnit5ExampleTestJUnit5OverviewTest {

//    Setup
    @BeforeAll
    static void beforeAll() {
        System.out.println("Before\nBefore all test methods \n");
//        Some examples of common expensive operations are:
//       - the creation of a database connection
//       - the startup of a server.
    }
 
    @BeforeEach
    void beforeEach() {
        System.out.println("  Before each test method");
//        This is useful when we want to execute some common code before running a test.
//        - list initialization
    }

//    Teardown
    @AfterEach
    void afterEach() {
        System.out.println("  After each test method \n");
    }
 
    @AfterAll
    static void afterAll() {
        System.out.println("After all test methods\n");
    }
 
// Added test methods

    @Test
    @DisplayName("First test")
    @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.

...

This tutorial has taught us four three things:

...

  • 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.