JUnit 5 Expected Exception - JUnit 151

JUnit 5 Expected Exception – JUnit 151

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

JUnit 5 Expected Exception

拋出預期例外,可以透過 lambda 表達式傳遞被測程式,若拋出預期的例外, 則 assertThrows 返回例外,並且對訊息進行斷言, JUnit Expected Exception 採用單元測試來驗證產出結果。

檔案目錄

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

assertThrows

該方法所提供的可執行區塊或 lambda 表達式,執行後會拋出 expectedType,它是一個重載方法,提供以下不同參數的方法:

static <T extends Throwable>T assertThrows(Class<T> expectedType, Executable executable)

static <T extends Throwable>T assertThrows(Class<T> expectedType, Executable executable, String message)

static <T extends Throwable>T assertThrows(Class<T> expectedType, Executable executable, Supplier<String> messageSupplier)
  1. expectedType 測試程式應該拋出這種類型的例外。

  2.  message 如果可執行程式沒有拋出任何異常,則此訊息將與 FAIL 結果一起印出。
  3. messageSupplier 如果測試失敗,將從中取得訊息。

單元測試

numberFormatException

JUnit Assertthrows 拋出數字格式例外。

	@Test
	public void numberFormatException() {
		Exception exception = assertThrows(NumberFormatException.class, () -> {
			Integer.parseInt("1A");
		});

		String expectedMessage = "For input string";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}
For input string: "1A"

runtimeException

JUnit 5 Assert Exception 拋出執行階段例外。

	@Test
	public void runtimeException() {
		Exception exception = assertThrows(RuntimeException.class, () -> {
			Integer.parseInt("1A");
		});

		String expectedMessage = "For input string";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}
For input string: "1A"

arithmeticException

JUnit 5 Assert Exception 拋出計算例外。

	@Test
	public void arithmeticException() {
		Exception exception = assertThrows(ArithmeticException.class, () -> {
			System.out.println(1 / 0);
		});

		String expectedMessage = "/ by zero";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}
/ by zero

nameNotFoundException

拋出自定義找不到名稱的例外。

	@Test
	void nameNotFoundException() {
		Exception exception = assertThrows(NameNotFoundException.class, () -> findByName("ruoxue"));

		String expectedMessage = "not found";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}

	public String findByName(String name) throws NameNotFoundException {
		throw new NameNotFoundException(name + " not found");
	}

	public class NameNotFoundException extends Exception {

		private static final long serialVersionUID = -2216858914752574276L;

		public NameNotFoundException(String message) {
			super(message);
		}
	}
ruoxue not found

JUnit5ExpectedExceptionTest.java

package org.ruoxue.spring_boot_168.test.junit5;

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

import org.junit.jupiter.api.Test;

public class JUnit5ExpectedExceptionTest {

	@Test
	public void numberFormatException() {
		Exception exception = assertThrows(NumberFormatException.class, () -> {
			Integer.parseInt("1A");
		});

		String expectedMessage = "For input string";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}

	@Test
	public void runtimeException() {
		Exception exception = assertThrows(RuntimeException.class, () -> {
			Integer.parseInt("1A");
		});

		String expectedMessage = "For input string";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}

	@Test
	public void arithmeticException() {
		Exception exception = assertThrows(ArithmeticException.class, () -> {
			System.out.println(1 / 0);
		});

		String expectedMessage = "/ by zero";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}

	@Test
	void nameNotFoundException() {
		Exception exception = assertThrows(NameNotFoundException.class, () -> findByName("ruoxue"));

		String expectedMessage = "not found";
		String actualMessage = exception.getMessage();
		System.out.println(actualMessage);

		assertTrue(actualMessage.contains(expectedMessage));
	}

	public String findByName(String name) throws NameNotFoundException {
		throw new NameNotFoundException(name + " not found");
	}

	public class NameNotFoundException extends Exception {

		private static final long serialVersionUID = -2216858914752574276L;

		public NameNotFoundException(String message) {
			super(message);
		}
	}
}

心得分享

JUnit 5 Test Exception 預期拋出例外的單元測試,可以幫助測試在 catch 區塊中撰寫的程式碼。

發佈留言