Spring Boot 2.2.0 introduced JUnit5 as the default unit testing library. Prior to this, spring-boot-starter-test used JUnit4. Replaced with JUnit Jupiter after Spring Boot 2.2.0.

The XxxApplicationTests unit test class is automatically generated after the Spring Boot project is successfully created. This class checks whether the application context can start properly. The @SpringBooTtest annotation finds a configuration class with the @SpringBootApplication annotation and uses that class to launch the application context.

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}
Copy the code

JUnit 5

JUnit5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

  • JUnit Platform: The foundation on which a test framework is launched on the JVM, supporting not only JUnit’s own test engine but also other test engines.
  • JUnit Jupiter: The core of JUnit5’s new feature, which contains a test engine running on the JUnit Platform.
  • JUnit Vintage: Provides JUnit 4.x and 3.x compatible test engines. Spring Boot 2.4 and above removes the default dependency on Vintage, and you need to import Vintage yourself if you want to use JUnit4.

Commonly used annotations

  • @test: indicates that the method is a Test method.

    @Test
    public void test() {
        Assertions.assertTrue(false);
    }
    Copy the code
  • @parameterizedtest: a ParameterizedTest that is run multiple times with different parameters.

    package com.example.demo; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; public class DemoUnitTest { @ParameterizedTest @ValueSource(ints = {1, 10, 100}) public void test(int value) { Assertions.assertTrue(value > 0 && value <= 100); }}Copy the code
  • @REPEATedTest: Repeat test. The value in parentheses is the number of repetitions.

    @RepeatedTest(5)
    public void test() {
        Assertions.assertTrue(true);
    }
    Copy the code
  • @displayName: Sets the name of the test class or test method to support emoji.

    package com.example.demo; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @displayName ("JUnit5 Demo") public class DemoUnitTest {@displayName @displayName ("JUnit5 Demo") public class DemoUnitTest {@displayName @displayName ("JUnit5 Demo") public class DemoUnitTest Assertions.assertTrue(true); }}Copy the code
  • @beforeeach: executes BeforeEach unit test method.

    package com.example.demo; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class DemoUnitTest { @BeforeEach public void beforeEach() { System.out.println("method[beforeEach] executed."); } @Test public void test1() { System.out.println("Unit test 1 executed."); } @Test public void test2() { System.out.println("Unit test 2 executed."); }}Copy the code

    The output

    method[beforeEach] executed.
    Unit test 1 executed.
    method[beforeEach] executed.
    Unit test 2 executed.
    Copy the code
  • AfterEach: execute AfterEach unit test method execution is complete.

    package com.example.demo; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; public class DemoUnitTest { @AfterEach public void AfterEach() { System.out.println("method[beforeEach] executed."); } @Test public void test1() { System.out.println("Unit test 1 executed."); } @Test public void test2() { System.out.println("Unit test 2 executed."); }}Copy the code

    The output

    Unit test 1 executed.
    method[AfterEach] executed.
    Unit test 2 executed.
    method[AfterEach] executed.
    Copy the code
  • BeforeAll: executed BeforeAll unit test methods are executed. Use only on static methods.

    package com.example.demo; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class DemoUnitTest { @BeforeAll public static void beforeAll() { System.out.println("method[beforeAll] executed."); } @Test public void test1() { System.out.println("Unit test 1 executed."); } @Test public void test2() { System.out.println("Unit test 2 executed."); }}Copy the code

    The output

    method[beforeAll] executed.
    Unit test 1 executed.
    Unit test 2 executed.
    Copy the code
  • AfterAll: execute AfterAll unit test methods have been executed, only on static methods.

    package com.example.demo; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; public class DemoUnitTest { @AfterAll public static void afterAll() { System.out.println("method[afterAll] executed."); } @Test public void test1() { System.out.println("Unit test 1 executed."); } @Test public void test2() { System.out.println("Unit test 2 executed."); }}Copy the code

    The output

    Unit test 1 executed.
    Unit test 2 executed.
    method[afterAll] executed.
    Copy the code
  • @tag: Tags the unit test category.

  • @disabled: Disabled is not executed, similar to @ignore in JUnit4.

  • @timeout: indicates the Timeout of a test method. If the Timeout period exceeds the specified time (unit: second), execution will fail.

    @Test
    @Timeout(1)
    public void test1() throws InterruptedException {
        Thread.currentThread().sleep(900);
        Assertions.assertEquals(0x111, 273);
    }
    Copy the code
  • @extendWith: Provides extended class references for test classes or test methods to replace RunWith annotations in older versions, but no additional configuration is required in the SpringBoot environment unless specifically required, because @SpringbooTtest already has them.

assertions

JUnit5 Assertions org. Junit. Jupiter. API. The Assertions of static methods. In addition you can also use AssertJ (org. AssertJ. Core. API. Assertions assertThat method).