Table of Contents
ToggleAssert Map Value in Java
驗證所有屬性,符合或不符合 Null 值,並透過 JUnit 5 單元測試來驗證產出結果,使用 Java Assert Map Value 流式斷言,可以大幅提升斷言效率,減少程式碼的撰寫,讓開發者體驗更流暢的驗證斷言。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- map
| +- AssertMapValueTest.java
單元測試
Assertions Map Value in Java 斷言映射的主要目的是取得映射的正確元素以進行斷言。
Fruit
Assertions Map 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 Map 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();
}
}
hasAllNullFieldsOrProperties
Assertions Map Value in Java 驗證所有屬性,符合 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasAllNullFieldsOrProperties() {
SimpleFruit grape = new SimpleFruit();
SimpleFruit kiwifruit = new SimpleFruit();
SimpleFruit lemon = new SimpleFruit();
Map<String, SimpleFruit> map = new HashMap<>();
map.put("Grape", grape);
map.put("Kiwifruit", kiwifruit);
map.put("Lemon", lemon);
System.out.println(map);
assertThat(map).allSatisfy((k, v) -> {
assertThat(v).hasAllNullFieldsOrProperties();
});
}
{Grape={"name":null,"origins":null}, Kiwifruit={"name":null,"origins":null}, Lemon={"name":null,"origins":null}}
hasAllNullFieldsOrPropertiesExcept
Asserting Map Value with Examples 驗證除了指定屬性之外,符合 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasAllNullFieldsOrPropertiesExcept() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
grape.setOrigins(null);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
kiwifruit.setOrigins(null);
Fruit lemon = new Fruit("Lemon", -1, 3);
lemon.setOrigins(null);
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).allSatisfy((k, v) -> {
assertThat(v).hasAllNullFieldsOrPropertiesExcept("name", "type", "quantity");
});
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1,"origins":null}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2,"origins":null}, Lemon={"name":"Lemon","quantity":-1.0,"type":3,"origins":null}}
hasNoNullFieldsOrProperties
Asserting Map Value with Examples 驗證所有屬性,符合非 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasNoNullFieldsOrProperties() {
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).allSatisfy((k, v) -> {
assertThat(v).hasNoNullFieldsOrProperties();
});
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1,"origins":[]}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2,"origins":[]}, Lemon={"name":"Lemon","quantity":-1.0,"type":3,"origins":[]}}
hasNoNullFieldsOrPropertiesExcept
Asserting Map Value with Examples 驗證除了指定屬性之外,符合非 Null 值,若不成立,則會拋出 AssertionError 。
@Test
public void hasNoNullFieldsOrPropertiesExcept() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
grape.setOrigins(null);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
kiwifruit.setOrigins(null);
Fruit lemon = new Fruit("Lemon", -1, 3);
lemon.setOrigins(null);
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).allSatisfy((k, v) -> {
assertThat(v).hasNoNullFieldsOrPropertiesExcept("origins");
});
}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1,"origins":null}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2,"origins":null}, Lemon={"name":"Lemon","quantity":-1.0,"type":3,"origins":null}}
AssertMapValueTest.java
Asserting Map Value with Examples 新增單元測試,驗證 Testing Java Map Value with AssertJ 是否符合預期。
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 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;
import static org.assertj.core.api.Assertions.assertThat;
public class AssertMapValueTest {
@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 hasAllNullFieldsOrProperties() {
SimpleFruit grape = new SimpleFruit();
SimpleFruit kiwifruit = new SimpleFruit();
SimpleFruit lemon = new SimpleFruit();
Map<String, SimpleFruit> map = new HashMap<>();
map.put("Grape", grape);
map.put("Kiwifruit", kiwifruit);
map.put("Lemon", lemon);
System.out.println(map);
assertThat(map).allSatisfy((k, v) -> {
assertThat(v).hasAllNullFieldsOrProperties();
});
}
@Test
public void hasAllNullFieldsOrPropertiesExcept() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
grape.setOrigins(null);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
kiwifruit.setOrigins(null);
Fruit lemon = new Fruit("Lemon", -1, 3);
lemon.setOrigins(null);
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).allSatisfy((k, v) -> {
assertThat(v).hasAllNullFieldsOrPropertiesExcept("name", "type", "quantity");
});
}
@Test
public void hasNoNullFieldsOrProperties() {
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).allSatisfy((k, v) -> {
assertThat(v).hasNoNullFieldsOrProperties();
});
}
@Test
public void hasNoNullFieldsOrPropertiesExcept() {
Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1);
grape.setOrigins(null);
Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
kiwifruit.setOrigins(null);
Fruit lemon = new Fruit("Lemon", -1, 3);
lemon.setOrigins(null);
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).allSatisfy((k, v) -> {
assertThat(v).hasNoNullFieldsOrPropertiesExcept("origins");
});
}
}
心得分享
Testing Java Map Value with AssertJ 除了提供流式判斷,還針對 Map 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈, Asserting Map Value with Examples 將有助於驗證效率的提升。