Table of Contents
ToggleAssertJ Array Assertions
提供驗證包含、不包含指定的元素等方法, AssertJ 是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Asserting Array with AssertJ 使用流式斷言,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- array
| +- AssertJArrayAssertionsTest.java
單元測試
Array Elements with AssertJ 斷言陣列的主要目的是取得陣列的正確元素以進行斷言。
contains
驗證包含指定的元素,若不成立,則會拋出 AssertionError 。
@Test
public void contains() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).contains("Guava", "Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).contains(1, 2, 3);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
doesNotContain
Array Elements with AssertJ 驗證不包含指定的元素,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContain() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContain("Mango");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContain(9);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
containsOnly
Array Elements with AssertJ 驗證包含全部元素,順序不需要保持一致,若不成立,則會拋出 AssertionError 。
@Test
public void containsOnly() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsOnly("Guava", "Pitaya", "Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnly(5, 4, 1, 2, 3);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
containsOnlyOnce
Array Elements with AssertJ 驗證包含只出現一次的元素,若不成立,則會拋出 AssertionError 。
@Test
public void containsOnlyOnce() {
String[] array = new String[] { "Durian", "Guava", "Pitaya"};
System.out.println(Arrays.toString(array));
assertThat(array).containsOnlyOnce("Guava");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnlyOnce(3);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
containsOnlyNulls
Array Elements with AssertJ 驗證只包含空值,若不成立,則會拋出 AssertionError 。
@Test
public void containsOnlyNulls() {
String[] array = new String[] { null, null, null };
System.out.println(Arrays.toString(array));
assertThat(array).containsOnlyNulls();
Integer[] intArray = new Integer[] { null, null, null, null, null };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnlyNulls();
}
[null, null, null]
[null, null, null, null, null]
containsAll
Testing Java Array with AssertJ 驗證包含全部元素,大小可以小於,若不成立,則會拋出 AssertionError 。
@Test
public void containsAll() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
List<String> list = Arrays.asList("Durian", "Guava");
System.out.println(Arrays.toString(array));
System.out.println(list);
assertThat(array).containsAll(list);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
List<Integer> intList = Arrays.asList(1, 2, 3);
System.out.println(Arrays.toString(intArray));
System.out.println(intList);
assertThat(intArray).containsAll(intList);
}
[Durian, Guava, Pitaya]
[Durian, Guava]
[1, 2, 3, 4, 5]
[1, 2, 3]
containsExactly
Testing Java Array with AssertJ 驗證包含全部元素,順序也需要保持一致,若不成立,則會拋出 AssertionError 。
@Test
public void containsExactly() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsExactly("Durian", "Guava", "Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnly(1, 2, 3, 4, 5);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
containsExactlyInAnyOrder
Testing Java Array with AssertJ 驗證包含全部元素,順序不需要保持一致,若不成立,則會拋出 AssertionError 。
@Test
public void containsExactlyInAnyOrder() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsExactlyInAnyOrder("Guava", "Pitaya", "Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsExactlyInAnyOrder(5, 4, 1, 2, 3);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
AssertJArrayAssertionsTest.java
Testing Java Array with AssertJ 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.array;
import static org.assertj.core.api.Assertions.*;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class AssertJArrayAssertionsTest {
@Test
public void contains() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).contains("Guava", "Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).contains(1, 2, 3);
}
@Test
public void doesNotContain() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContain("Mango");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContain(9);
}
@Test
public void containsOnly() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsOnly("Guava", "Pitaya", "Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnly(5, 4, 1, 2, 3);
}
@Test
public void containsOnlyOnce() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsOnlyOnce("Guava");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnlyOnce(3);
}
@Test
public void containsOnlyNulls() {
String[] array = new String[] { null, null, null };
System.out.println(Arrays.toString(array));
assertThat(array).containsOnlyNulls();
Integer[] intArray = new Integer[] { null, null, null, null, null };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnlyNulls();
}
@Test
public void containsAll() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
List<String> list = Arrays.asList("Durian", "Guava");
System.out.println(Arrays.toString(array));
System.out.println(list);
assertThat(array).containsAll(list);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
List<Integer> intList = Arrays.asList(1, 2, 3);
System.out.println(Arrays.toString(intArray));
System.out.println(intList);
assertThat(intArray).containsAll(intList);
}
@Test
public void containsExactly() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsExactly("Durian", "Guava", "Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsOnly(1, 2, 3, 4, 5);
}
@Test
public void containsExactlyInAnyOrder() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsExactlyInAnyOrder("Guava", "Pitaya", "Durian");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsExactlyInAnyOrder(5, 4, 1, 2, 3);
}
}
心得分享
Assert Array Java 除了提供流式判斷,還針對 Array 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈,善用 Testing Java Array with AssertJ 將有助於驗證效率的提升。