Table of Contents
ToggleUnit Test AssertJ Array with Examples
許多測試驗證場景,提供開發者更流暢的驗證 API,驗證更直覺,使用流式斷言,不須再物件自己呼叫方法,流暢地寫完驗證,不須再寫迴圈, AssertJ Array Unit Test with Examples 介紹 containsAnyOf 、 containsAtIndex 、 containsSequence 、 containsNull 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- array
| +- UnitTestAssertJArrayWithExamplesTest.java
單元測試
Examples AssertJ Array Unit Test 斷言陣列的主要目的是取得陣列的正確元素以進行斷言。
containsAnyOf
驗證包含任一元素,若不成立,則會拋出 AssertionError 。
@Test
public void containsAnyOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsAnyOf("Durian", "Mango");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsAnyOf(1, 9);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
containsAtIndex
Examples AssertJ Array Unit Test 驗證指定索引的元素,若不成立,則會拋出 AssertionError 。
@Test
public void containsAtIndex() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).contains("Durian", atIndex(0)).contains("Guava", atIndex(1)).contains("Pitaya", atIndex(2));
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).contains(1, atIndex(0)).contains(3, atIndex(2));
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
containsSequence
Examples Unit Test AssertJ Array 驗證依序包含元素,若不成立,則會拋出 AssertionError 。
@Test
public void containsSequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsSequence("Guava", "Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsSequence(4, 5);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
doesNotContainSequence
Examples Unit Test AssertJ Array 驗證不依序包含元素,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainSequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContainSequence("Durian", "Pitaya");
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContainSequence(1, 5);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
containsSubsequence
Examples Unit Test AssertJ Array 驗證依序包含元素,若不成立,則會拋出 AssertionError 。
@Test
public void containsSubsequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsSubsequence("Durian", "Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsSubsequence(1, 5);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
doesNotContainSubsequence
Examples Unit Test AssertJ Array 驗證不依序包含元素,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainSubsequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContainSubsequence("Pitaya", "Durian");
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContainSubsequence(5, 1);
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
doesNotContainAnyElementsOf
Examples Unit Test AssertJ Array 驗證不包含任何元素,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainAnyElementsOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
List<String> list = Arrays.asList("Mango", "Orange");
System.out.println(Arrays.toString(array));
System.out.println(list);
assertThat(array).doesNotContainAnyElementsOf(list);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
List<Integer> intList = Arrays.asList(8, 9);
System.out.println(Arrays.toString(intArray));
System.out.println(intList);
assertThat(intArray).doesNotContainAnyElementsOf(intList);
}
[Durian, Guava, Pitaya]
[Mango, Orange]
[1, 2, 3, 4, 5]
[8, 9]
containsNull
Examples Unit Test AssertJ Array 驗證包含 null 元素,若不成立,則會拋出 AssertionError 。
@Test
public void containsNull() {
String[] array = new String[] { "Durian", "Guava", null };
System.out.println(Arrays.toString(array));
assertThat(array).containsNull();
Integer[] intArray = new Integer[] { 1, 2, 3, 4, null };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsNull();
}
[Durian, Guava, null]
[1, 2, 3, 4, null]
doesNotContainNull
Examples Unit Test AssertJ Array 驗證不包含 null 元素,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainNull() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContainNull();
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContainNull();
}
[Durian, Guava, Pitaya]
[1, 2, 3, 4, 5]
UnitTestAssertJArrayWithExamplesTest.java
Examples Unit Test AssertJ Array 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.array;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.atIndex;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class UnitTestAssertJArrayWithExamplesTest {
@Test
public void containsAnyOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsAnyOf("Durian", "Mango");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsAnyOf(1, 9);
}
@Test
public void containsAtIndex() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).contains("Durian", atIndex(0)).contains("Guava", atIndex(1)).contains("Pitaya", atIndex(2));
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).contains(1, atIndex(0)).contains(3, atIndex(2));
}
@Test
public void containsSequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsSequence("Guava", "Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsSequence(4, 5);
}
@Test
public void doesNotContainSequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContainSequence("Durian", "Pitaya");
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContainSequence(1, 5);
}
@Test
public void containsSubsequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).containsSubsequence("Durian", "Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsSubsequence(1, 5);
}
@Test
public void doesNotContainSubsequence() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContainSubsequence("Pitaya", "Durian");
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContainSubsequence(5, 1);
}
@Test
public void doesNotContainAnyElementsOf() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
List<String> list = Arrays.asList("Mango", "Orange");
System.out.println(Arrays.toString(array));
System.out.println(list);
assertThat(array).doesNotContainAnyElementsOf(list);
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
List<Integer> intList = Arrays.asList(8, 9);
System.out.println(Arrays.toString(intArray));
System.out.println(intList);
assertThat(intArray).doesNotContainAnyElementsOf(intList);
}
@Test
public void containsNull() {
String[] array = new String[] { "Durian", "Guava", null };
System.out.println(Arrays.toString(array));
assertThat(array).containsNull();
Integer[] intArray = new Integer[] { 1, 2, 3, 4, null };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsNull();
}
@Test
public void doesNotContainNull() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
System.out.println(Arrays.toString(array));
assertThat(array).doesNotContainNull();
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(intArray));
assertThat(intArray).doesNotContainNull();
}
}
心得分享
Unit Test AssertJ Array Example 斷言的基本方法是 assertThat 方法,提高了測試程式碼的可讀性,並且被設計為在主流 IDE 中易於使用,讓開發者將繁瑣的測試驗證過程,變為更簡單,善用 Examples Unit Test AssertJ Array 將有助於驗證效率的提升。