AssertJ Map Assertions - AssertJ 155

AssertJ Map Assertions – AssertJ 155

AssertJ Map Assertions

驗證元素包含、不包含指定的 Key 、 Value 等方法,使用流式斷言,可以大幅提升斷言效率,減少程式碼的撰寫, Asserting Map with AssertJ 讓開發者體驗更流暢的驗證斷言,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

Map Elements with AssertJ 斷言映射的主要目的是取得映射的正確元素以進行斷言。

containsKey

驗證包含指定的元素 Key 值,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsKey() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsKey("Kiwifruit");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsKey(2);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

doesNotContainKey

Map Elements with AssertJ 驗證不包含指定的元素 Key 值,若不成立,則會拋出 AssertionError 。

	@Test
	public void doesNotContainKey() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).doesNotContainKey("Papaya");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).doesNotContainKey(9);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

containsKeys

Map Elements with AssertJ 驗證包含指定的元素 Key 值,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsKeys() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsKeys("Grape", "Kiwifruit");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsKeys(1, 2);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

doesNotContainKeys

Map Elements with AssertJ 驗證不包含指定的元素 Key 值,若不成立,則會拋出 AssertionError 。

	@Test
	public void doesNotContainKeys() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).doesNotContainKeys("Papaya", "Strawberry");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).doesNotContainKeys(8, 9);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

containsOnlyKeys

Map Elements with AssertJ 驗證包含全部 Key 值,順序不需要保持一致,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsOnlyKeys() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		List<String> keys = Arrays.asList("Lemon", "Grape", "Kiwifruit");
		assertThat(map).containsOnlyKeys(keys);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		List<Integer> intKeys = Arrays.asList(3, 1, 2);
		assertThat(intMap).containsOnlyKeys(intKeys);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

containsValues

Testing Java Map with AssertJ 驗證包含指定的元素 Value 值,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsValue() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsValue(19);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsValue(19);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

doesNotContainValue

Testing Java Map with AssertJ 驗證不包含指定的元素 Value 值,若不成立,則會拋出 AssertionError 。

	@Test
	public void doesNotContainValue() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).doesNotContainValue(30);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).doesNotContainValue(99);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

containsValues

Testing Java Map with AssertJ 驗證包含指定的元素 Value 值,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsValues() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsValues(18, 19);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsValues(18, 19);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

AssertJMapAssertionsTest.java

Testing Java Map with AssertJ 新增單元測試,驗證 Assert Map Java  是否符合預期。

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

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

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class AssertJMapAssertionsTest {

	@Test
	public void containsKey() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsKey("Kiwifruit");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsKey(2);
	}

	@Test
	public void doesNotContainKey() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).doesNotContainKey("Papaya");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).doesNotContainKey(9);
	}

	@Test
	public void containsKeys() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsKeys("Grape", "Kiwifruit");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsKeys(1, 2);
	}

	@Test
	public void doesNotContainKeys() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).doesNotContainKeys("Papaya", "Strawberry");

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).doesNotContainKeys(8, 9);
	}

	@Test
	public void containsOnlyKeys() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		List<String> keys = Arrays.asList("Lemon", "Grape", "Kiwifruit");
		assertThat(map).containsOnlyKeys(keys);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		List<Integer> intKeys = Arrays.asList(3, 1, 2);
		assertThat(intMap).containsOnlyKeys(intKeys);
	}

	@Test
	public void containsValue() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsValue(19);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsValue(19);
	}

	@Test
	public void doesNotContainValue() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).doesNotContainValue(30);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).doesNotContainValue(99);
	}

	@Test
	public void containsValues() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsValues(18, 19);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsValues(18, 19);
	}
}

心得分享

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

發佈留言