Table of Contents
ToggleAsserting Maps with AssertJ
驗證符合滿足條件的元素,並透過 JUnit 5 單元測試來驗證產出結果,使用 Assert Maps in Java 流式斷言,可以大幅提升斷言效率,減少程式碼的撰寫,讓開發者體驗更流暢的驗證斷言。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- map
| +- AssertingMapsTest.java
單元測試
Assertions Maps in Java 斷言映射的主要目的是取得映射的正確元素以進行斷言。
Fruit
Assertions Maps 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();
}
}
matches
Assertions Maps in Java 驗證符合條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void matches() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).matches(m -> m.get("Kiwifruit") != null);
Predicate<Map<String, Fruit>> keyQuantity = m -> m.entrySet().stream()
.anyMatch((Map.Entry<String, Fruit> e) -> "Kiwifruit".equals(e.getKey())
&& Double.compare(e.getValue().getQuantity(), 1d) == 0);
assertThat(map).matches(keyQuantity, "keyQuantity");
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}
satisfies
Assertions Maps in Java 驗證符合條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void satisfies() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
Condition<Map<String, Fruit>> size = new Condition<Map<String, Fruit>>(m -> m.size() > 2, "size");
assertThat(map).satisfies(size);
Predicate<Map<String, Fruit>> predcate = m -> m.entrySet().stream()
.anyMatch((Map.Entry<String, Fruit> e) -> "Kiwifruit".equals(e.getKey())
&& Double.doubleToLongBits(e.getValue().getQuantity()) == Double.doubleToLongBits(1d));
Condition<Map<String, Fruit>> keyQuantity = new Condition<Map<String, Fruit>>(predcate, "keyQuantity");
assertThat(map).satisfies(keyQuantity);
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}
satisfiesAnyOf
Assertions Maps in Java 驗證任一符合條件的元素,若不成立,則會拋出 AssertionError 。
@Test
public void satisfiesAnyOf() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).satisfiesAnyOf(m -> {
assertThat(m.get("Kiwifruit")).isNotNull();
});
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}
hasFieldOrProperty
Asserting Maps with Examples 驗證符合 Key 的元素,若不成立,則會拋出 AssertionError 。
@Test
public void hasFieldOrProperty() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).hasFieldOrProperty("Kiwifruit");
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}
hasFieldOrPropertyWithValue
Asserting Maps with Examples 驗證符合 Key 及 Value 的元素,若不成立,則會拋出 AssertionError 。
@Test
public void hasFieldOrPropertyWithValue() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).hasFieldOrPropertyWithValue("Kiwifruit", kiwifruit);
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}
AssertingMapsTest.java
Asserting Maps with Examples 新增單元測試,驗證 Testing Java Maps with AssertJ是否符合預期,使用 AssertJMap 流式斷言。
package org.ruoxue.spring_boot_168.test.assertj.map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
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.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import static org.assertj.core.api.Assertions.assertThat;
public class AssertingMapsTest {
@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();
}
}
@Test
public void matches() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).matches(m -> m.get("Kiwifruit") != null);
Predicate<Map<String, Fruit>> keyQuantity = m -> m.entrySet().stream()
.anyMatch((Map.Entry<String, Fruit> e) -> "Kiwifruit".equals(e.getKey())
&& Double.compare(e.getValue().getQuantity(), 1d) == 0);
assertThat(map).matches(keyQuantity, "keyQuantity");
}
@Test
public void satisfies() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
Condition<Map<String, Fruit>> size = new Condition<Map<String, Fruit>>(m -> m.size() > 2, "size");
assertThat(map).satisfies(size);
Predicate<Map<String, Fruit>> predcate = m -> m.entrySet().stream()
.anyMatch((Map.Entry<String, Fruit> e) -> "Kiwifruit".equals(e.getKey())
&& Double.doubleToLongBits(e.getValue().getQuantity()) == Double.doubleToLongBits(1d));
Condition<Map<String, Fruit>> keyQuantity = new Condition<Map<String, Fruit>>(predcate, "keyQuantity");
assertThat(map).satisfies(keyQuantity);
}
@Test
public void satisfiesAnyOf() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).satisfiesAnyOf(m -> {
assertThat(m.get("Kiwifruit")).isNotNull();
});
}
@Test
public void hasFieldOrProperty() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).hasFieldOrProperty("Kiwifruit");
}
@Test
public void hasFieldOrPropertyWithValue() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
Fruit lemon = new Fruit("Lemon", -1, 3);
Map<String, Fruit> map = new HashMap<>();
map.put(grape.getName(), grape);
map.put(kiwifruit.getName(), kiwifruit);
map.put(lemon.getName(), lemon);
System.out.println(map);
assertThat(map).hasFieldOrPropertyWithValue("Kiwifruit", kiwifruit);
}
}
心得分享
Testing Java Maps with AssertJ 除了提供流式判斷,還針對 Map 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈, Asserting Maps with Examples 將有助於驗證效率的提升。