[JAVA] How to use JUnit 5

This article is from MDC Advent Calendar 2020 Day 19.

Introduction

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.

Overview

What is JUnit 5

—— 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

――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"
}

Supported Java versions

--Java 8 or above is required to run JUnit5

How to write a test

Test method

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.

Pre-processing / post-processing

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

Test grouping

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.

Parameter test

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

Finally

――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

reference

Recommended Posts

How to use JUnit 5
How to use JUnit (beginner)
[Creating] How to use JUnit
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Map
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use Ruby return
[Rails] How to use enum
[Swift] How to use UserDefaults
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to write Junit 5 organized
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
How to migrate from JUnit4 to JUnit5
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)
How to use scope (JSP & Servlet)