JUnit 5 Test - JUnit 151

JUnit 5 Test – JUnit 151

  • Post author:
  • Post category:RD / JUnit
  • Post comments:0 Comments
  • Post last modified:2023-02-19

JUnit 5 Test

執行測試的程式,設定該測試需要重複的次數,顯示自定義的名稱等,採用單元測試來驗證產出結果。

檔案目錄

./
   +- build.gradle
       +- src
           +- test
           |   +- org
           |       +- ruoxue
           |           +- spring_boot_168
           |               +- test
           |                   +- junit5
           |                       +- JUnit5Test.java   

註解說明

提供多樣化的註解,可以使用在測試類別和方法上。

AnnotationsDescription
@Test宣告為測試方法。
@DisplayName顯示自訂說明。
@RepeatedTest重複測試。

單元測試

JUnit 5 Test

執行測試的程式,會依照設置程式碼的先後順序,優先執行排列較上面的 @Test 程式,此註解不帶任何屬性。

    @Test
	public void helloWorld() {
		System.out.println("Hello World");
	}
Hello World

JUnit 5 RepeatedTest

透過註解方法,設定該測試需要重複的次數,進行測試,每次調用都像 @Test 方法一樣,可放入 name 的屬性輸出目前的次數與測試訊息。

	@BeforeAll
	public static void beforeAll()  {
		System.out.println("beforeAll");
	}

	@AfterAll
	public static void afterAll() {
		System.out.println("afterAll");
	}

	@BeforeEach
	public void beforeEach() {
		System.out.println("beforeEach");
	}

	@AfterEach
	public void afterEach() throws Exception {
		System.out.println("afterEach");
	}

	@RepeatedTest(3)
	public void repeatedHelloWorld() {
		System.out.println("Hello World");
	}
	
	@RepeatedTest(3)
	public void repeatedJavaWorld() {
		System.out.println("Java World");
	}
beforeAll

beforeEach
Java World
afterEach

beforeEach
Java World
afterEach

beforeEach
Java World
afterEach

beforeEach
Hello World
afterEach

beforeEach
Hello World
afterEach

beforeEach
Hello World
afterEach

afterAll

可自訂顯示訊息。

{displayName}: 測試方法名稱。
{currentRepetition}: 目前重複次數。
{totalRepetitions}: 總共重複次數。

	@RepeatedTest(value = 3, name = "{displayName} {currentRepetition}/{totalRepetitions}")
	public void repeatedHelloWorld_2() {
		System.out.println("Hello World");
	}
	
	@RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
	public void repeatedHelloWorld_3() {
		System.out.println("Hello World");
	}
RepeatedTest JUnit 151
LONG_DISPLAY_NAME JUnit 151

JUnit 5 RepetitionInfo

重複測試資訊。

	@RepeatedTest(value = 3)
	public void repeatedHelloWorld_4(RepetitionInfo repetitionInfo) {
		System.out.println("Hello World");
		System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
		assertEquals(3, repetitionInfo.getTotalRepetitions());
	}
beforeEach
Hello World
Repetition #1
afterEach

beforeEach
Hello World
Repetition #2
afterEach

beforeEach
Hello World
Repetition #3
afterEach

JUnit 5 DisplayName

測試類別和方法可以使用,會顯示在輸出結果上。

@DisplayName("JUnit World")
public class JUnit5Test {

	@DisplayName("Hello World")
	@Test
	public void helloWorld() {
		System.out.println("Hello World");
	}

	@DisplayName("Java World")
	@Test
	public void javaWorld() {
		System.out.println("Java World");
	}
}
Hello World
Java World

JUnit5Test.java

package org.ruoxue.spring_boot_168.test.junit5;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;

@DisplayName("JUnit World")
public class JUnit5Test {

	@BeforeAll
	public static void beforeAll() {
		System.out.println("beforeAll");
	}

	@AfterAll
	public static void afterAll() {
		System.out.println("afterAll");
	}

	@BeforeEach
	public void beforeEach() {
		System.out.println("beforeEach");
	}

	@AfterEach
	public void afterEach() throws Exception {
		System.out.println("afterEach");
	}

	@DisplayName("Hello World")
	@Test
	public void helloWorld() {
		System.out.println("Hello World");
	}

	@DisplayName("Java World")
	@Test
	public void javaWorld() {
		System.out.println("Java World");
	}
	
	@RepeatedTest(3)
	public void repeatedHelloWorld() {
		System.out.println("Hello World");
	}

	@RepeatedTest(3)
	public void repeatedJavaWorld() {
		System.out.println("Java World");
	}

	@RepeatedTest(value = 3, name = "{displayName} {currentRepetition}/{totalRepetitions}")
	public void repeatedHelloWorld_2() {
		System.out.println("Hello World");
	}

	@RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
	public void repeatedHelloWorld_3() {
		System.out.println("Hello World");
	}

	@RepeatedTest(value = 3)
	public void repeatedHelloWorld_4(RepetitionInfo repetitionInfo) {
		System.out.println("Hello World");
		System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
		assertEquals(3, repetitionInfo.getTotalRepetitions());
	}
}
DisplayName JUnit 151

心得分享

JUnit 5 Tests 撰寫出適合系統業務需求的測試程式,每一次更新就會增加一段給新功能的測試,增加不同例外狀況模擬執行,可以確保系統保持在一個穩定的狀況中成長。

發佈留言