Table of Contents
ToggleUnit Test Array with AssertJ
提供了一組豐富的斷言和真正有用的錯誤訊息,提高了測試程式碼的可讀性,通常與 JUnit 測試一起使用, Unit Test Java AssertJ Array 流式斷言,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- array
| +- UnitTestArrayWithAssertJTest.java
單元測試
Java AssertJ Array Unit Test 斷言陣列的主要目的是取得陣列的正確元素以進行斷言。
is
驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void is() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
Condition<String[]> length = new Condition<String[]>(s -> s.length > 2, "length");
System.out.println(Arrays.toString(array));
assertThat(array).is(length);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
Condition<int[]> intLength = new Condition<int[]>(i -> i.length > 2, "length");
assertThat(intArray).is(intLength);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
isNot
Java AssertJ Array Unit Test 驗證是否不符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void isNot() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
Condition<String[]> length = new Condition<String[]>(s -> s.length > 3, "length");
assertThat(array).isNot(length);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
Condition<int[]> intLength = new Condition<int[]>(i -> i.length > 5, "length");
assertThat(intArray).isNot(intLength);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
isIn
Java AssertJ Array Unit Test 驗證是否包含於陣列中,若不成立,則會拋出 AssertionError 。
@Test
public void isIn() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat("Guava").isIn(array);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(3).isIn(intArray);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
isNotIn
Java AssertJ Array Unit Test 驗證是否不包含於陣列中,若不成立,則會拋出 AssertionError 。
@Test
public void isNotIn() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat("Mango").isNotIn(array);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(9).isNotIn(intArray);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
isInstanceOf
Java AssertJ Array Unit Test 驗證是否為指定類別實例,若不成立,則會拋出 AssertionError 。
@Test
public void isInstanceOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).isInstanceOf(String[].class);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).isInstanceOf(int[].class);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
isNotInstanceOf
Java AssertJ Array Unit Test 驗證是否不為指定類別實例,若不成立,則會拋出 AssertionError 。
@Test
public void isNotInstanceOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).isNotInstanceOf(String.class);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).isNotInstanceOf(int.class);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
startsWith
Java Unit Test AssertJ Array 驗證是否為第一個元素,若不成立,則會拋出 AssertionError 。
@Test
public void startsWith() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).startsWith("Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).startsWith(1);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
endsWith
Java Unit Test AssertJ Array 驗證是否為最後個元素,若不成立,則會拋出 AssertionError 。
@Test
public void endsWith() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).endsWith("Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).endsWith(5);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
UnitTestArrayWithAssertJTest.java
Java Unit Test AssertJ Array 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.array;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
public class UnitTestArrayWithAssertJTest {
@Test
public void is() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
Condition<String[]> length = new Condition<String[]>(s -> s.length > 2, "length");
System.out.println(Arrays.toString(array));
assertThat(array).is(length);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
Condition<int[]> intLength = new Condition<int[]>(i -> i.length > 2, "length");
assertThat(intArray).is(intLength);
}
@Test
public void isNot() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
Condition<String[]> length = new Condition<String[]>(s -> s.length > 3, "length");
assertThat(array).isNot(length);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
Condition<int[]> intLength = new Condition<int[]>(i -> i.length > 5, "length");
assertThat(intArray).isNot(intLength);
}
@Test
public void isIn() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat("Guava").isIn(array);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(3).isIn(intArray);
}
@Test
public void isNotIn() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat("Mango").isNotIn(array);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(9).isNotIn(intArray);
}
@Test
public void isInstanceOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).isInstanceOf(String[].class);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).isInstanceOf(int[].class);
}
@Test
public void isNotInstanceOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).isNotInstanceOf(String.class);
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).isNotInstanceOf(int.class);
}
@Test
public void startsWith() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).startsWith("Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).startsWith(1);
}
@Test
public void endsWith() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).endsWith("Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).endsWith(5);
}
}
心得分享
AssertJ Array Java Unit Test 斷言的基本方法是 assertThat 方法,提高了測試程式碼的可讀性,並且被設計為在主流 IDE 中易於使用,讓開發者將繁瑣的測試驗證過程,變為更簡單,善用 Java Unit Test AssertJ Array 將有助於驗證效率的提升。