JUnit 5 BeforeAll - JUnit 151

JUnit 5 BeforeAll – JUnit 151

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

JUnit 5 BeforeAll

用於定義物件或參數處理,釋放資源或整理結果,或是每個方法執行前後執行前置檢查、釋放資源等, 採用單元測試來驗證產出結果。

檔案目錄

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

註解說明

提供多樣化的註解宣告。

JUnit 5DescriptionJUnit 4
@BeforeAll需為靜態方法,在所有測試方法前執行。@BeforeClass
@AfterAll需為靜態方法,在所有測試方法後執行。@AfterClass
@BeforeEach在每個測試方法前執行。@Before
@AfterEach在每個測試方法後執行。@After

單元測試

JUnit 5 BeforeAll

在該類別執行所有 @Test 的程式前,用於全域性資料和外部資源的初始化、定義物件或參數處理,使用 @RepeatedTest 執行 2 次,@BeforeAll 只會在開始時執行一次。
	@BeforeAll
	public static void beforeAll() throws Exception {
		System.out.println("beforeAll");
	}
	
	@RepeatedTest(value = 2)
	public void javaWorld() {
		System.out.println("Java World");
	}
beforeAll

Java World
Java World

JUnit 5 AfterAll

在該類別執行所有 @Test 的程式後,用於釋放資源或整理結果,使用 @RepeatedTest 執行 2 次,@AfterAll 會在結束測試後執行一次。
	@RepeatedTest(value = 2)
	public void javaWorld() {
		System.out.println("Java World");
	}	
	
	@AfterAll
	public static void afterAll() throws Exception {
		System.out.println("afterAll");
	}
Java World
Java World

afterAll

JUnit 5 BeforeEach

在任何 @Test 執行前,先行運行的程式,負責該測試用例所需要的執行環境及重整資源,使用 @RepeatedTest 執行 2 次,@BeforeEach 會在測試用例執行前,執行一次。

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

	@RepeatedTest(value = 2)
	public void javaWorld() {
		System.out.println("Java World");
	}
beforeEach
Java World

beforeEach
Java World

JUnit 5 AfterEach

在任何 @Test 執行後,才運行的程式,負責該測試用例印出結果或釋放資源,使用 @RepeatedTest 執行 2 次,@AfterEach 會在測試用例執行後,執行一次。

	@RepeatedTest(value = 2)
	public void javaWorld() {
		System.out.println("Java World");
	}	
	
	@AfterEach
	public void afterEach() throws Exception {
		System.out.println("afterEach");
	}
Java World
afterEach

Java World
afterEach

JUnit5BeforeAllTest.java

package org.ruoxue.spring_boot_168.test.junit5;

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.RepeatedTest;

public class JUnit5BeforeAllTest {

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

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

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

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

	@RepeatedTest(value = 2)
	public void javaWorld() {
		System.out.println("Java World");
	}
}

javaWorld

測試方法上點右鍵執行 Run As -> JUnit Test ,查看 console。

beforeAll

beforeEach
Java World
afterEach

beforeEach
Java World
afterEach

afterAll

故障排除

BeforeAll 錯誤

執行 JUnit Test,拋出例外,發生錯誤,stack trace 如下:

org.junit.platform.commons.JUnitException: @BeforeAll method 'public void test.JUnit5Test.setupBeforeAll() throws java.lang.Exception' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:57)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findMethodsAndAssertStatic$0(LifecycleMethodUtils.java:81)

這是因為此註解的方法,需要宣告為靜態方法。

//修改前
	@BeforeAll
	public void setupBeforeAll() throws Exception {
		System.out.println("1. BeforeAll");
	}

//修改後
	@BeforeAll
	public static void setupBeforeAll() throws Exception {
		System.out.println("1. BeforeAll");
	}

AfterAll 錯誤

執行 JUnit Test,拋出例外,發生錯誤,stack trace 如下:

org.junit.platform.commons.JUnitException: @AfterAll method 'public void test.JUnit5Test.tearDownAfterAll() throws java.lang.Exception' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:57)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findMethodsAndAssertStatic$0(LifecycleMethodUtils.java:81)

這是因為此註解的方法,需要宣告為靜態方法。

//修改前
	@AfterAll
	public void tearDownAfterAll() throws Exception {
		System.out.println("4. AfterAll");
	}

//修改後
	@AfterAll
	public static void tearDownAfterAll() throws Exception {
		System.out.println("4. AfterAll");
	}

心得分享

Assertions JUnit 使用單元測試可以幫助驗證程式是否符合實際結果,其實撰寫單元測試,通常會額外花費大量的時間,這個付出是值得的,避免日後更版出現更多問題,可以即時發現問題。

發佈留言