Asserting Maps in Java with Examples - AssertJ 155

Asserting Maps in Java with Examples – AssertJ 155

Asserting Maps in Java with Examples

提供了一組豐富的斷言和真正有用的錯誤訊息,提高了測試程式碼的可讀性,驗證符合、滿足條件的元素, Asserting Maps in Java 提供常見的方法,如: allMatch 、 allSatisfy 等,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- spring_boot_168
       |               +- test
       |                   +- assertj
       |                       +- map
       |                           +- AssertingMapsWithExamplesTest.java   

單元測試

Asserting Maps Java 斷言陣列的主要目的是取得陣列的正確元素以進行斷言。

Fruit

Asserting Maps Java 建立 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();
		}
	}

allSatisfy

Asserting Maps Java 驗證所有滿足條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void allSatisfy() {
		Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 2);
		Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
		Fruit lemon = new Fruit("Lemon", -1, 2);
		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.getType()).isEqualTo(2);
			assertThat(v.getName()).isNotNull();
		});
	}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":2}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":2}}

anySatisfy

Asserting Maps Java 驗證任一滿足條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void anySatisfy() {
		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).anySatisfy((k, v) -> {
			assertThat(v.getType()).isEqualTo(2);
			assertThat(v.getName()).isEqualTo("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}}

noneSatisfy

Asserting Maps Java 驗證不滿足條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void noneSatisfy() {
		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).noneSatisfy((k, v) -> {
			assertThat(v.getType()).isEqualTo(0);
			assertThat(v.getName().length()).isLessThan(5);
		});
	}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}

hasEntrySatisfying

Java Asserting Maps 驗證 Entry 滿足條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void hasEntrySatisfying() {
		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.Entry<String, Fruit>> lengthQuantity = new Condition<>(
				e -> e.getKey().length() > 6 && e.getValue().getQuantity() > 0, "length quantity");
		assertThat(map).hasEntrySatisfying(lengthQuantity);
	}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}

hasKeySatisfying

Java Asserting Maps 驗證 Key 滿足條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void hasKeySatisfying() {
		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<String> length = new Condition<String>(s -> s.length() > 6, "length");
		assertThat(map).hasKeySatisfying(length);
	}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":1.0,"type":2}, {"name":"Cherry","quantity":-1.0,"type":3}]

hasValueSatisfying

Java Asserting Maps 驗證 Value 滿足條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void hasValueSatisfying() {
		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<Fruit> quantity = new Condition<Fruit>(o -> o.quantity > 0, "quantity");
		assertThat(map).hasValueSatisfying(quantity);
	}
{Grape={"name":"Grape","quantity":1.7976931348623157E308,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":1.0,"type":2}, Lemon={"name":"Lemon","quantity":-1.0,"type":3}}

AssertingMapsWithExamplesTest.java

Java Asserting Maps 新增單元測試,驗證是否符合預期。

package org.ruoxue.spring_boot_168.test.assertj.map;

import static org.assertj.core.api.Assertions.*;

import java.util.HashMap;
import java.util.Map;

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 AssertingMapsWithExamplesTest {

	@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 allSatisfy() {
		Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 2);
		Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2);
		Fruit lemon = new Fruit("Lemon", -1, 2);
		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.getType()).isEqualTo(2);
			assertThat(v.getName()).isNotNull();
		});
	}

	@Test
	public void anySatisfy() {
		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).anySatisfy((k, v) -> {
			assertThat(v.getType()).isEqualTo(2);
			assertThat(v.getName()).isEqualTo("Kiwifruit");
		});
	}

	@Test
	public void noneSatisfy() {
		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).noneSatisfy((k, v) -> {
			assertThat(v.getType()).isEqualTo(0);
			assertThat(v.getName().length()).isLessThan(5);
		});
	}

	@Test
	public void hasEntrySatisfying() {
		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.Entry<String, Fruit>> lengthQuantity = new Condition<>(
				e -> e.getKey().length() > 6 && e.getValue().getQuantity() > 0, "length quantity");
		assertThat(map).hasEntrySatisfying(lengthQuantity);
	}

	@Test
	public void hasKeySatisfying() {
		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<String> length = new Condition<String>(s -> s.length() > 6, "length");
		assertThat(map).hasKeySatisfying(length);
	}

	@Test
	public void hasValueSatisfying() {
		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<Fruit> quantity = new Condition<Fruit>(o -> o.quantity > 0, "quantity");
		assertThat(map).hasValueSatisfying(quantity);
	}
}

心得分享

Java Asserting Maps Example 除了提供流式判斷,還針對 Array 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈,善用 Java Asserting Maps 將有助於驗證效率的提升。

發佈留言