Table of Contents
ToggleAsserting Lists Methods
驗證 List 抽取滿足條件的元素,提供了一組豐富的斷言和真正有用的錯誤訊息,提高了測試程式碼的可讀性, Lists Methods Asserting 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- list
| +- AssertingListsMethodsTest.java
單元測試
Lists Methods AssertJ 斷言集合的主要目的是取得集合的正確元素以進行斷言。
Fruit
Lists Methods AssertJ 建立 Fruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
private List<String> origins = new ArrayList<>();
public Fruit(String name, double quantity, int type, List<String> origins) {
this.name = name;
this.quantity = quantity;
this.type = type;
this.origins = origins;
}
public Fruit(String name, double quantity, int type) {
this(name, quantity, type, new ArrayList<>());
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
builder.append("origins", origins);
return builder.toString();
}
}
extracting
Lists Methods AssertJ 驗證提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void extracting() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting(e -> e.getName()).contains("Apple", "Banana");
assertThat(list).extracting(e -> e.getType()).containsOnly(1, 2, 3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
extractingWithFieldName
Lists Methods AssertJ 驗證取得根據類名或方法引用或屬性名反射調用,或根據 lambda 表達式調用,提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void extractingWithFieldName() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting("name").containsAll(Arrays.asList("Apple", "Banana"));
assertThat(list).extracting("type").containsExactly(1, 2, 3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
extractingWithMethodReference
Lists Methods AssertJ 驗證依方法參考調用,提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void extractingWithMethodReference() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting(Fruit::getName).containsExactlyInAnyOrder("Cherry", "Apple", "Banana");
assertThat(list).extracting(Fruit::getType).containsAnyOf(1, 2);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
extractingTuple
Lists Methods with AssertJ 驗證將一組屬性的值包裝成組合,提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void extractingTuple() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting("name", "quantity").containsSequence(tuple("Banana", 1d), tuple("Cherry", -1d));
assertThat(list).extracting(e -> e.getName(), Fruit::getQuantity)
.containsSubsequence(tuple("Apple", Double.MAX_VALUE), tuple("Cherry", -1d));
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
extractingResultOf
Lists Methods with AssertJ 驗證依方法提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void extractingResultOf() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extractingResultOf("getName").doesNotContainNull();
assertThat(list).extractingResultOf("getQuantity", Double.class).hasSize(3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
flatExtracting
Lists Methods with AssertJ 驗證取得根據類名或方法引用或屬性名反射調用,或根據 lambda 表達式調用,提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void flatExtracting() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
Fruit banana = new Fruit("Banana", 1, 2, Arrays.asList("Canada", "Norway"));
Fruit cherry = new Fruit("Cherry", -1, 3, Arrays.asList("Poland", "Japan"));
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).flatExtracting("origins").containsOnly("Australia", "Canada", "Norway", "Poland", "Japan");
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[Australia]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[Canada, Norway]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[Poland, Japan]}]
flatExtractingWithMethodReference
Lists Methods with AssertJ 驗證依方法參考調用,提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void flatExtractingWithMethodReference() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
Fruit banana = new Fruit("Banana", 1, 2, Arrays.asList("Canada", "Norway"));
Fruit cherry = new Fruit("Cherry", -1, 3, Arrays.asList("Poland", "Japan"));
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).flatExtracting(Fruit::getOrigins).containsOnly("Australia", "Canada", "Norway", "Poland",
"Japan");
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[Australia]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[Canada, Norway]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[Poland, Japan]}]
AssertingListsMethodsTest.java
Lists Methods with AssertJ 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.list;
import static org.assertj.core.api.Assertions.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.jupiter.api.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class AssertingListsMethodsTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
private List<String> origins = new ArrayList<>();
public Fruit(String name, double quantity, int type, List<String> origins) {
this.name = name;
this.quantity = quantity;
this.type = type;
this.origins = origins;
}
public Fruit(String name, double quantity, int type) {
this(name, quantity, type, new ArrayList<>());
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
builder.append("origins", origins);
return builder.toString();
}
}
@Test
public void extracting() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting(e -> e.getName()).contains("Apple", "Banana");
assertThat(list).extracting(e -> e.getType()).containsOnly(1, 2, 3);
}
@Test
public void extractingWithFieldName() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting("name").containsAll(Arrays.asList("Apple", "Banana"));
assertThat(list).extracting("type").containsExactly(1, 2, 3);
}
@Test
public void extractingWithMethodReference() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting(Fruit::getName).containsExactlyInAnyOrder("Cherry", "Apple", "Banana");
assertThat(list).extracting(Fruit::getType).containsAnyOf(1, 2);
}
@Test
public void extractingTuple() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extracting("name", "quantity").containsSequence(tuple("Banana", 1d), tuple("Cherry", -1d));
assertThat(list).extracting(e -> e.getName(), Fruit::getQuantity)
.containsSubsequence(tuple("Apple", Double.MAX_VALUE), tuple("Cherry", -1d));
}
@Test
public void extractingResultOf() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).extractingResultOf("getName").doesNotContainNull();
assertThat(list).extractingResultOf("getQuantity", Double.class).hasSize(3);
}
@Test
public void flatExtracting() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
Fruit banana = new Fruit("Banana", 1, 2, Arrays.asList("Canada", "Norway"));
Fruit cherry = new Fruit("Cherry", -1, 3, Arrays.asList("Poland", "Japan"));
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).flatExtracting("origins").containsOnly("Australia", "Canada", "Norway", "Poland", "Japan");
}
@Test
public void flatExtractingWithMethodReference() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
Fruit banana = new Fruit("Banana", 1, 2, Arrays.asList("Canada", "Norway"));
Fruit cherry = new Fruit("Cherry", -1, 3, Arrays.asList("Poland", "Japan"));
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).flatExtracting(Fruit::getOrigins).containsOnly("Australia", "Canada", "Norway", "Poland",
"Japan");
}
}
心得分享
Lists Functions with AssertJ 除了提供流式判斷,還針對 Array 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈,善用 Lists Methods with AssertJ 將有助於驗證效率的提升。