Asserting Maps Methods - AssertJ 155

Asserting Maps Methods – AssertJ 155

Asserting Maps Methods

驗證 Map 抽取滿足條件的元素,提供了一組豐富的斷言和真正有用的錯誤訊息,提高了測試程式碼的可讀性, Maps Methods Asserting 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

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

Fruit

Maps Methods AssertJ 建立 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();
		}
	}

extracting

Maps Methods AssertJ 驗證依照 Key 提取符合條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void extracting() {
		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).extracting(m -> m.get("Grape"), m -> m.get("Kiwifruit")).contains(grape, kiwifruit);
		assertThat(map).extracting(m -> m.get("Papaya")).isNull();
	}
{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":[]}}

extractingWithKey

Maps Methods AssertJ 驗證依照 Key 提取符合條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void extractingWithKey() {
		Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
		Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2, Arrays.asList("Canada", "Norway"));
		Fruit lemon = new Fruit("Lemon", -1, 3, Arrays.asList("Poland", "Japan"));
		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).extracting("Grape", "Kiwifruit").containsOnly(grape, kiwifruit);
		assertThat(map).extracting("Lemon").extracting("name").isEqualTo("Lemon");
	}
{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":[]}}

extractingByKey

Maps Methods with AssertJ 驗證依照 Key 提取符合條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void extractingByKey() {
		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 LinkedHashMap<String, Fruit>();
		map.put(grape.getName(), grape);
		map.put(kiwifruit.getName(), kiwifruit);
		map.put(lemon.getName(), lemon);
		System.out.println(map);
		assertThat(map).extractingByKey("Grape").extracting(Fruit::getName, as(InstanceOfAssertFactories.STRING))
				.isEqualTo("Grape");
		assertThat(map).extractingByKey("Lemon").extracting(Fruit::getType, as(InstanceOfAssertFactories.INTEGER))
				.isEqualTo(3);
	}
{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":[]}}

extractingByKeys

Maps Methods with AssertJ 驗證依照多個 Key 提取符合條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void extractingByKeys() {
		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 LinkedHashMap<String, Fruit>();
		map.put(grape.getName(), grape);
		map.put(kiwifruit.getName(), kiwifruit);
		map.put(lemon.getName(), lemon);
		System.out.println(map);
		assertThat(map).extractingByKeys("Grape", "Kiwifruit").containsOnly(grape, kiwifruit);
		assertThat(map).extractingByKeys("Papaya", "Lemon").contains(lemon);
	}
{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":[]}}

extractingFromEntries

Maps Methods with AssertJ 驗證依照 Entry 提取符合條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void extractingFromEntries() {
		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 LinkedHashMap<String, Fruit>();
		map.put(grape.getName(), grape);
		map.put(kiwifruit.getName(), kiwifruit);
		map.put(lemon.getName(), lemon);
		System.out.println(map);
		Function<Map.Entry<String, Fruit>, Object> value = e -> e.getValue();
		assertThat(map).extractingFromEntries(value).containsOnly(grape, kiwifruit, lemon);
		Function<Map.Entry<String, Fruit>, Object> type = e -> e.getValue().getType();
		assertThat(map).extractingFromEntries(type).containsOnly(1, 2, 3);
	}
{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":[]}}

flatExtracting

Maps Methods with AssertJ 驗證提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。

	@Test
	public void flatExtracting() {
		Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
		Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2, Arrays.asList("Canada", "Norway"));
		Fruit lemon = new Fruit("Lemon", -1, 3, Arrays.asList("Poland", "Japan"));
		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).flatExtracting("Grape", "Kiwifruit").containsOnly(grape, kiwifruit);
	}
{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":[]}}

AssertingMapsMethodsTest.java

Maps Methods with AssertJ 新增單元測試,驗證 Maps Functions with AssertJ 是否符合預期。

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

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class AssertingMapsMethodsTest {

	@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();
		}
	}

	@Test
	public void extracting() {
		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).extracting(m -> m.get("Grape"), m -> m.get("Kiwifruit")).contains(grape, kiwifruit);
		assertThat(map).extracting(m -> m.get("Papaya")).isNull();
	}

	@Test
	public void extractingWithKey() {
		Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
		Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2, Arrays.asList("Canada", "Norway"));
		Fruit lemon = new Fruit("Lemon", -1, 3, Arrays.asList("Poland", "Japan"));
		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).extracting("Grape", "Kiwifruit").containsOnly(grape, kiwifruit);
		assertThat(map).extracting("Lemon").extracting("name").isEqualTo("Lemon");
	}

	@Test
	public void extractingByKey() {
		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 LinkedHashMap<String, Fruit>();
		map.put(grape.getName(), grape);
		map.put(kiwifruit.getName(), kiwifruit);
		map.put(lemon.getName(), lemon);
		System.out.println(map);
		assertThat(map).extractingByKey("Grape").extracting(Fruit::getName, as(InstanceOfAssertFactories.STRING))
				.isEqualTo("Grape");
		assertThat(map).extractingByKey("Lemon").extracting(Fruit::getType, as(InstanceOfAssertFactories.INTEGER))
				.isEqualTo(3);
	}

	@Test
	public void extractingByKeys() {
		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 LinkedHashMap<String, Fruit>();
		map.put(grape.getName(), grape);
		map.put(kiwifruit.getName(), kiwifruit);
		map.put(lemon.getName(), lemon);
		System.out.println(map);
		assertThat(map).extractingByKeys("Grape", "Kiwifruit").containsOnly(grape, kiwifruit);
		assertThat(map).extractingByKeys("Papaya", "Lemon").contains(lemon);
	}

	@Test
	public void extractingFromEntries() {
		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 LinkedHashMap<String, Fruit>();
		map.put(grape.getName(), grape);
		map.put(kiwifruit.getName(), kiwifruit);
		map.put(lemon.getName(), lemon);
		System.out.println(map);
		Function<Map.Entry<String, Fruit>, Object> value = e -> e.getValue();
		assertThat(map).extractingFromEntries(value).containsOnly(grape, kiwifruit, lemon);
		Function<Map.Entry<String, Fruit>, Object> type = e -> e.getValue().getType();
		assertThat(map).extractingFromEntries(type).containsOnly(1, 2, 3);
	}

	@Test
	public void flatExtracting() {
		Fruit grape = new Fruit("Grape", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
		Fruit kiwifruit = new Fruit("Kiwifruit", 1, 2, Arrays.asList("Canada", "Norway"));
		Fruit lemon = new Fruit("Lemon", -1, 3, Arrays.asList("Poland", "Japan"));
		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).flatExtracting("Grape", "Kiwifruit").containsOnly(grape, kiwifruit);
	}
}

心得分享

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

發佈留言