Table of Contents
ToggleAssertJ Exception Assertions
驗證例外拋出、包含訊息、是否為實例等方法,並透過單元測試來驗證產出結果,使用 Asserting Exceptions with AssertJ 流式斷言,可以大幅提升斷言效率,減少程式碼的撰寫。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- exception
| +- AssertJExceptionAssertionsTest.java
單元測試
Exception with AssertJ 斷言例外的主要目的是取得例外以進行斷言。
isInstanceOf
驗證拋出例外,是否為指定類別。
protected int divide(int value1, int value2) {
return value1 / value2;
}
@Test
public void isInstanceOf() {
List<String> list = Arrays.asList("AssertJ", "155");
assertThatThrownBy(() -> {
list.get(2);
}).isInstanceOf(IndexOutOfBoundsException.class);
assertThatThrownBy(() -> divide(1, 0)).isInstanceOf(ArithmeticException.class);
assertThatCode(() -> list.get(2)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatCode(() -> divide(1, 0)).isInstanceOf(ArithmeticException.class);
}
hasMessage
Exception with AssertJ 驗證拋出例外,是否為包含、符合訊息。
@Test
public void hasMessage() {
List<String> list = Arrays.asList("AssertJ", "155");
assertThatThrownBy(() -> {
list.get(2);
}).hasMessage("2").hasMessageContaining("2").hasMessageMatching("2");
assertThatThrownBy(() -> divide(1, 0)).hasMessage("/ by zero").hasMessageContaining("zero")
.hasMessageMatching("/ by zero");
assertThatCode(() -> list.get(2)).hasMessage("2").hasMessageContaining("2").hasMessageMatching("2");
assertThatCode(() -> divide(1, 0)).hasMessage("/ by zero").hasMessageContaining("zero")
.hasMessageMatching("/ by zero");
}
hasCauseInstanceOf
Exception with AssertJ 驗證拋出例外的原因,是否為指定類別。
@Test
public void hasCauseInstanceOf() {
assertThatThrownBy(() -> {
try {
throw new IOException();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(IOException.class)
.hasStackTraceContaining("IOException");
assertThatCode(() -> {
try {
throw new IOException();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(IOException.class)
.hasStackTraceContaining("IOException");
}
hasCause
Testing Java Exception with AssertJ 驗證拋出例外的原因。
@Test
public void hasCause() {
Exception cause = new Exception("cause");
Exception runtimeException = new RuntimeException("runtime", cause);
assertThat(runtimeException).hasCause(cause).hasCauseReference(cause).hasCauseInstanceOf(Exception.class);
cause = new NullPointerException("null");
Throwable throwable = new Throwable(cause);
assertThat(throwable).hasCause(cause).hasCauseInstanceOf(NullPointerException.class)
.hasCauseInstanceOf(RuntimeException.class).hasCauseExactlyInstanceOf(NullPointerException.class);
}
cause
Testing Java Exception with AssertJ 驗證拋出例外的原因,是否有訊息或匹配訊息。
@Test
public void cause() {
Exception cause = new Exception("cause");
Exception runtimeException = new RuntimeException("runtime", cause);
assertThat(runtimeException).cause().hasMessage("cause").hasMessageMatching("ca.se");
cause = new NullPointerException("null");
Throwable throwable = new Throwable(cause);
assertThat(throwable).cause().hasMessage("null").hasMessageMatching("nu..");
}
hasRootCause
Testing Java Exception with AssertJ 驗證拋出例外的根原因。
@Test
public void hasRootCause() {
Exception cause = new Exception("cause");
Exception parentCause = new IllegalStateException(cause);
Exception runtimeException = new RuntimeException("runtime", parentCause);
assertThat(runtimeException).hasRootCause(cause).hasRootCauseInstanceOf(Exception.class).hasCause(parentCause)
.hasCauseInstanceOf(IllegalStateException.class);
cause = new NullPointerException("null");
parentCause = new IllegalStateException(cause);
Throwable throwable = new Throwable(parentCause);
assertThat(throwable).hasRootCause(cause).hasRootCauseInstanceOf(NullPointerException.class)
.hasRootCauseInstanceOf(RuntimeException.class)
.hasRootCauseExactlyInstanceOf(NullPointerException.class).hasCause(parentCause)
.hasCauseInstanceOf(IllegalStateException.class);
}
AssertJExceptionAssertionsTest.java
Testing Java Exception with AssertJ 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.exception;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class AssertJExceptionAssertionsTest {
protected int divide(int value1, int value2) {
return value1 / value2;
}
@Test
public void isInstanceOf() {
List<String> list = Arrays.asList("AssertJ", "155");
assertThatThrownBy(() -> {
list.get(2);
}).isInstanceOf(IndexOutOfBoundsException.class);
assertThatThrownBy(() -> divide(1, 0)).isInstanceOf(ArithmeticException.class);
assertThatCode(() -> list.get(2)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatCode(() -> divide(1, 0)).isInstanceOf(ArithmeticException.class);
}
@Test
public void hasMessage() {
List<String> list = Arrays.asList("AssertJ", "155");
assertThatThrownBy(() -> {
list.get(2);
}).hasMessage("2").hasMessageContaining("2").hasMessageMatching("2");
assertThatThrownBy(() -> divide(1, 0)).hasMessage("/ by zero").hasMessageContaining("zero")
.hasMessageMatching("/ by zero");
assertThatCode(() -> list.get(2)).hasMessage("2").hasMessageContaining("2").hasMessageMatching("2");
assertThatCode(() -> divide(1, 0)).hasMessage("/ by zero").hasMessageContaining("zero")
.hasMessageMatching("/ by zero");
}
@Test
public void hasCauseInstanceOf() {
assertThatThrownBy(() -> {
try {
throw new IOException();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(IOException.class)
.hasStackTraceContaining("IOException");
assertThatCode(() -> {
try {
throw new IOException();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(IOException.class)
.hasStackTraceContaining("IOException");
}
@Test
public void hasCause() {
Exception cause = new Exception("cause");
Exception runtimeException = new RuntimeException("runtime", cause);
assertThat(runtimeException).hasCause(cause).hasCauseReference(cause).hasCauseInstanceOf(Exception.class);
cause = new NullPointerException("null");
Throwable throwable = new Throwable(cause);
assertThat(throwable).hasCause(cause).hasCauseInstanceOf(NullPointerException.class)
.hasCauseInstanceOf(RuntimeException.class).hasCauseExactlyInstanceOf(NullPointerException.class);
}
@Test
public void cause() {
Exception cause = new Exception("cause");
Exception runtimeException = new RuntimeException("runtime", cause);
assertThat(runtimeException).cause().hasMessage("cause").hasMessageMatching("ca.se");
cause = new NullPointerException("null");
Throwable throwable = new Throwable(cause);
assertThat(throwable).cause().hasMessage("null").hasMessageMatching("nu..");
}
@Test
public void hasRootCause() {
Exception cause = new Exception("cause");
Exception parentCause = new IllegalStateException(cause);
Exception runtimeException = new RuntimeException("runtime", parentCause);
assertThat(runtimeException).hasRootCause(cause).hasRootCauseInstanceOf(Exception.class).hasCause(parentCause)
.hasCauseInstanceOf(IllegalStateException.class);
cause = new NullPointerException("null");
parentCause = new IllegalStateException(cause);
Throwable throwable = new Throwable(parentCause);
assertThat(throwable).hasRootCause(cause).hasRootCauseInstanceOf(NullPointerException.class)
.hasRootCauseInstanceOf(RuntimeException.class)
.hasRootCauseExactlyInstanceOf(NullPointerException.class).hasCause(parentCause)
.hasCauseInstanceOf(IllegalStateException.class);
}
}
心得分享
Assert Exception Java 除了提供流式判斷,還針對 Exception 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,善用 Testing Java Exception with AssertJ 將有助於驗證效率的提升。