Table of Contents
ToggleAsserting Lists with AssertJ
提供了一組豐富的斷言和真正有用的錯誤訊息,驗證 List 過濾滿足條件的元素,Assert Lists in Java 提高了測試程式碼的可讀性 ,可以大幅提升斷言效率,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- list
| +- AssertingListsTest.java
單元測試
Assertions Lists in Java 斷言集合的主要目的是取得集合的正確元素以進行斷言。
Fruit
Assertions Lists in Java 建立 Fruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
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);
return builder.toString();
}
}
filteredOn
Assertions Lists in Java 使用 Function ,驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void filteredOn() {
int expectedSize = 1;
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).filteredOn(Fruit::getName, "Banana").hasSize(expectedSize);
assertThat(list).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":1.0,"type":2}, {"name":"Cherry","quantity":-1.0,"type":3}]
filteredOnWithPredicate
Assertions Lists in Java 使用 Predicate , 驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void filteredOnWithPredicate() {
int expectedSize = 2;
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).filteredOn(e -> e.getName().length() > 5).hasSize(expectedSize);
assertThat(list).filteredOn(e -> e.getQuantity() > 0).hasSize(expectedSize);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":1.0,"type":2}, {"name":"Cherry","quantity":-1.0,"type":3}]
filteredOnWithFieldName
Assertions Lists in Java 使用 field name , 驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void filteredOnWithFieldName() {
int expectedSize = 1;
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).filteredOn("name", "Banana").hasSize(expectedSize);
assertThat(list).filteredOn("type", 2).hasSize(expectedSize);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":1.0,"type":2}, {"name":"Cherry","quantity":-1.0,"type":3}]
filteredOnWithFilterOperator
Asserting Lists with examples 使用 FilterOperator , 驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void filteredOnWithFilterOperator() {
int expectedSize = 1;
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).filteredOn("name", in("Banana", "Cherry")).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
assertThat(list).filteredOn("type", notIn(1, 2)).filteredOn(e -> e.getQuantity() < 0).hasSize(expectedSize);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":1.0,"type":2}, {"name":"Cherry","quantity":-1.0,"type":3}]
filteredOnWithCondition
Asserting Lists with examples 使用 Condition ,驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void filteredOnWithCondition() {
int expectedSize = 2;
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);
Condition<Fruit> length = new Condition<Fruit>(o -> o.name.length() > 5, "length");
assertThat(list).filteredOn(length).hasSize(expectedSize);
Condition<Fruit> quantity = new Condition<Fruit>(o -> o.quantity > 0, "quantity");
assertThat(list).filteredOn(quantity).hasSize(expectedSize);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":1.0,"type":2}, {"name":"Cherry","quantity":-1.0,"type":3}]
filteredOnAssertions
Asserting Lists with examples 使用 ThrowingConsumer 驗證邏輯時,發生錯誤,拋出例外
@Test
public void filteredOnAssertions() {
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).filteredOnAssertions(e -> {
double result = 1 / 0;
});
}
java.lang.ArithmeticException: / by zero
at org.ruoxue.spring_boot_168.test.assertj.list.AssertingListsTest.lambda$8(AssertingListsTest.java:116)
at org.assertj.core.api.ThrowingConsumer.accept(ThrowingConsumer.java:30)
filteredOnNull
Testing Java Lists with AssertJ 驗證符合空值的元素屬性,若不成立,則會拋出 AssertionError 。
@Test
public void filteredOnNull() {
int expectedSize = 1;
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit(null, 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).filteredOnNull("name").hasSize(expectedSize);
}
[{"name":"Durian","quantity":1.7976931348623157E308,"type":1}, {"name":null,"quantity":1.0,"type":2}, {"name":"Pitaya","quantity":-1.0,"type":3}]
AssertingListsTest.java
Asserting Lists with Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.list;
import static org.assertj.core.api.Assertions.*;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class AssertingListsTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
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);
return builder.toString();
}
}
@Test
public void filteredOn() {
int expectedSize = 1;
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).filteredOn(Fruit::getName, "Banana").hasSize(expectedSize);
assertThat(list).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
}
@Test
public void filteredOnWithPredicate() {
int expectedSize = 2;
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).filteredOn(e -> e.getName().length() > 5).hasSize(expectedSize);
assertThat(list).filteredOn(e -> e.getQuantity() > 0).hasSize(expectedSize);
}
@Test
public void filteredOnWithFieldName() {
int expectedSize = 1;
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).filteredOn("name", "Banana").hasSize(expectedSize);
assertThat(list).filteredOn("type", 2).hasSize(expectedSize);
}
@Test
public void filteredOnWithFilterOperator() {
int expectedSize = 1;
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).filteredOn("name", in("Banana", "Cherry")).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
assertThat(list).filteredOn("type", notIn(1, 2)).filteredOn(e -> e.getQuantity() < 0).hasSize(expectedSize);
}
@Test
public void filteredOnWithCondition() {
int expectedSize = 2;
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);
Condition<Fruit> length = new Condition<Fruit>(o -> o.name.length() > 5, "length");
assertThat(list).filteredOn(length).hasSize(expectedSize);
Condition<Fruit> quantity = new Condition<Fruit>(o -> o.quantity > 0, "quantity");
assertThat(list).filteredOn(quantity).hasSize(expectedSize);
}
@Test
public void filteredOnAssertions() {
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).filteredOnAssertions(e -> {
double result = 1 / 0;
});
}
@Test
public void filteredOnNull() {
int expectedSize = 1;
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit(null, 1, 2);
Fruit cherry = new Fruit("Cherry", -1, 3);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).filteredOnNull("name").hasSize(expectedSize);
}
}
心得分享
Testing Java Lists with AssertJ 除了提供流式判斷,還針對 List 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈,善用 Asserting Lists with Examples 將有助於驗證效率的提升。