Table of Contents
ToggleUnit Test Class with AssertJ
介紹 is 、 isIn 、 isInstanceOf 等方法,提供了一組豐富的斷言和真正有用的錯誤訊息,提高了測試程式碼的可讀性,通常與 JUnit 測試一起使用, Unit Test Java AssertJ Class 流式斷言,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- clazz
| +- UnitTestClassWithAssertJTest.java
單元測試
Java AssertJ Class Unit Test 斷言類別的主要目的是取得類別以進行斷言。
is
驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void is() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
Condition<Class<?>> iface = new Condition<Class<?>>(c -> c.isInterface(), "interface");
assertThat(clazz).is(iface);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
Condition<Class<?>> primitive = new Condition<Class<?>>(c -> c.isPrimitive(), "primitive");
assertThat(clazz2).is(primitive);
}
interface java.lang.CharSequence
int
isNot
Java AssertJ Class Unit Test 驗證是否不符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void isNot() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
Condition<Class<?>> iface = new Condition<Class<?>>(c -> c.isPrimitive(), "interface");
assertThat(clazz).isNot(iface);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
Condition<Class<?>> primitive = new Condition<Class<?>>(c -> c.isInterface(), "primitive");
assertThat(clazz2).isNot(primitive);
}
interface java.lang.CharSequence
int
isIn
Java AssertJ Class Unit Test 驗證是否包含於陣列中,若不成立,則會拋出 AssertionError 。
@Test
public void isIn() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).isIn(CharSequence.class, String.class);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).isIn(int.class, Integer.class);
}
interface java.lang.CharSequence
int
isNotIn
Java AssertJ Class Unit Test 驗證是否不包含於陣列中,若不成立,則會拋出 AssertionError 。
@Test
public void isNotIn() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).isNotIn(int.class, Integer.class);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).isNotIn(CharSequence.class, String.class);
}
interface java.lang.CharSequence
int
isInstanceOf
Java AssertJ Class Unit Test 驗證是否為指定類別實例,若不成立,則會拋出 AssertionError 。
@Test
public void isInstanceOf() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(new String()).isInstanceOf(clazz);
Class<?> clazz2 = Integer.class;
System.out.println(clazz2);
assertThat(1).isInstanceOf(clazz2);
}
interface java.lang.CharSequence
class java.lang.Integer
isNotInstanceOf
Java AssertJ Class Unit Test 驗證是否不為指定類別實例,若不成立,則會拋出 AssertionError 。
@Test
public void isNotInstanceOf() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(1).isNotInstanceOf(clazz);
Class<?> clazz2 = Integer.class;
System.out.println(clazz2);
assertThat(new String()).isNotInstanceOf(clazz2);
}
interface java.lang.CharSequence
class java.lang.Integer
UnitTestClassWithAssertJTest.java
Java Unit Test AssertJ Class 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.clazz;
import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
public class UnitTestClassWithAssertJTest {
@Test
public void is() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
Condition<Class<?>> iface = new Condition<Class<?>>(c -> c.isInterface(), "interface");
assertThat(clazz).is(iface);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
Condition<Class<?>> primitive = new Condition<Class<?>>(c -> c.isPrimitive(), "primitive");
assertThat(clazz2).is(primitive);
}
@Test
public void isNot() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
Condition<Class<?>> iface = new Condition<Class<?>>(c -> c.isPrimitive(), "interface");
assertThat(clazz).isNot(iface);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
Condition<Class<?>> primitive = new Condition<Class<?>>(c -> c.isInterface(), "primitive");
assertThat(clazz2).isNot(primitive);
}
@Test
public void isIn() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).isIn(CharSequence.class, String.class);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).isIn(int.class, Integer.class);
}
@Test
public void isNotIn() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).isNotIn(int.class, Integer.class);
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).isNotIn(CharSequence.class, String.class);
}
@Test
public void isInstanceOf() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(new String()).isInstanceOf(clazz);
Class<?> clazz2 = Integer.class;
System.out.println(clazz2);
assertThat(1).isInstanceOf(clazz2);
}
@Test
public void isNotInstanceOf() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(1).isNotInstanceOf(clazz);
Class<?> clazz2 = Integer.class;
System.out.println(clazz2);
assertThat(new String()).isNotInstanceOf(clazz2);
}
}
心得分享
AssertJ Class Java Unit Test 斷言的基本方法是 assertThat 方法,提高了測試程式碼的可讀性,並且被設計為在主流 IDE 中易於使用,讓開發者將繁瑣的測試驗證過程,變為更簡單,善用 Java Unit Test AssertJ Class 將有助於驗證效率的提升。