Table of Contents
ToggleAssert List Value in Java
驗證所有屬性,符合或不符合 Null 值,並透過 JUnit 5 單元測試來驗證產出結果,使用 Java Assert List Value 流式斷言,可以大幅提升斷言效率,減少程式碼的撰寫,讓開發者體驗更流暢的驗證斷言。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- list
| +- AssertListValueTest.java
單元測試
Assertions List Value in Java 斷言集合的主要目的是取得集合的正確元素以進行斷言。
Fruit
Assertions List Value in Java 建立 Fruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
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) {
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);
builder.append("origins", origins);
return builder.toString();
}
}
SimpleFruit
Assertions List Value in Java 建立 SimpleFruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。
@Getter
@Setter
public static class SimpleFruit {
private String name;
private List<String> origins;
public SimpleFruit() {
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("origins", origins);
return builder.toString();
}
}
matches
Assertions List Value in Java 驗證符合條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void matches() {
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).matches(l -> l.size() > 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":[]}]
satisfies
Assertions List Value in Java 驗證符合條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void satisfies() {
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).satisfies(l -> {
assertThat(l.get(0).getName().equals("Apple"));
assertThat(l.get(1)).isNotNull();
});
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
satisfiesAnyOf
Assertions List Value in Java 驗證任一符合條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void satisfiesAnyOf() {
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).satisfiesAnyOf(l -> {
assertThat(l.get(0).getName().equals("Apple"));
}, l -> {
assertThat(l.get(1)).isNull();
});
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
hasAllNullFieldsOrProperties
Assertions List Value in Java 驗證所有屬性,符合 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasAllNullFieldsOrProperties() {
SimpleFruit apple = new SimpleFruit();
SimpleFruit banana = new SimpleFruit();
SimpleFruit cherry = new SimpleFruit();
List<SimpleFruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).allSatisfy(e -> {
assertThat(e).hasAllNullFieldsOrProperties();
});
}
[{"name":null,"origins":null}, {"name":null,"origins":null}, {"name":null,"origins":null}]
hasAllNullFieldsOrPropertiesExcept
Asserting List Value with Examples 驗證除了指定屬性之外,符合 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasAllNullFieldsOrPropertiesExcept() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
apple.setOrigins(null);
Fruit banana = new Fruit("Banana", 1, 2);
banana.setOrigins(null);
Fruit cherry = new Fruit("Cherry", -1, 3);
cherry.setOrigins(null);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).allSatisfy(e -> {
assertThat(e).hasAllNullFieldsOrPropertiesExcept("name", "type", "quantity");
});
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":null}, {"name":"Banana","quantity":1.0,"type":2,"origins":null}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":null}]
hasNoNullFieldsOrProperties
Asserting List Value with Examples 驗證所有屬性,符合非 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasNoNullFieldsOrProperties() {
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).allSatisfy(e -> {
assertThat(e).hasNoNullFieldsOrProperties();
});
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[]}]
hasNoNullFieldsOrPropertiesExcept
Asserting List Value with Examples 驗證除了指定屬性之外,符合非 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasNoNullFieldsOrPropertiesExcept() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
apple.setOrigins(null);
Fruit banana = new Fruit("Banana", 1, 2);
banana.setOrigins(null);
Fruit cherry = new Fruit("Cherry", -1, 3);
cherry.setOrigins(null);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).allSatisfy(e -> {
assertThat(e).hasNoNullFieldsOrPropertiesExcept("origins");
});
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":null}, {"name":"Banana","quantity":1.0,"type":2,"origins":null}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":null}]
AssertListValueTest.java
Asserting List Value with Examples 新增單元測試,驗證 Testing Java List Value with AssertJ 是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.list;
import static org.assertj.core.api.Assertions.assertThat;
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.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class AssertListValueTest {
@NoArgsConstructor
@Getter
@Setter
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) {
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);
builder.append("origins", origins);
return builder.toString();
}
}
@Getter
@Setter
public static class SimpleFruit {
private String name;
private List<String> origins;
public SimpleFruit() {
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("origins", origins);
return builder.toString();
}
}
@Test
public void matches() {
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).matches(l -> l.size() > 2);
}
@Test
public void satisfies() {
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).satisfies(l -> {
assertThat(l.get(0).getName().equals("Apple"));
assertThat(l.get(1)).isNotNull();
});
}
@Test
public void satisfiesAnyOf() {
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).satisfiesAnyOf(l -> {
assertThat(l.get(0).getName().equals("Apple"));
}, l -> {
assertThat(l.get(1)).isNull();
});
}
@Test
public void hasAllNullFieldsOrProperties() {
SimpleFruit apple = new SimpleFruit();
SimpleFruit banana = new SimpleFruit();
SimpleFruit cherry = new SimpleFruit();
List<SimpleFruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).allSatisfy(e -> {
assertThat(e).hasAllNullFieldsOrProperties();
});
}
@Test
public void hasAllNullFieldsOrPropertiesExcept() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
apple.setOrigins(null);
Fruit banana = new Fruit("Banana", 1, 2);
banana.setOrigins(null);
Fruit cherry = new Fruit("Cherry", -1, 3);
cherry.setOrigins(null);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).allSatisfy(e -> {
assertThat(e).hasAllNullFieldsOrPropertiesExcept("name", "type", "quantity");
});
}
@Test
public void hasNoNullFieldsOrProperties() {
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).allSatisfy(e -> {
assertThat(e).hasNoNullFieldsOrProperties();
});
}
@Test
public void hasNoNullFieldsOrPropertiesExcept() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
apple.setOrigins(null);
Fruit banana = new Fruit("Banana", 1, 2);
banana.setOrigins(null);
Fruit cherry = new Fruit("Cherry", -1, 3);
cherry.setOrigins(null);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
System.out.println(list);
assertThat(list).allSatisfy(e -> {
assertThat(e).hasNoNullFieldsOrPropertiesExcept("origins");
});
}
}
心得分享
Testing Java List Value with AssertJ 除了提供流式判斷,還針對 List 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈, Asserting List Value with Examples 將有助於驗證效率的提升。