Unit Test AssertJ Map with Examples - AssertJ 155

Unit Test AssertJ Map with Examples – AssertJ 155

Unit Test AssertJ Map with Examples

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

檔案目錄

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

單元測試

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

containsAnyOf

Examples AssertJ Map Unit Test 驗證包含任一元素,若不成立,則會拋出 AssertionError 。

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

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

containsEntry

Examples AssertJ Map Unit Test 驗證包含指定的元素,若不成立,則會拋出 AssertionError 。

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

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

doesNotContainEntry

Examples AssertJ Map Unit Test 驗證不包含指定的元素,若不成立,則會拋出 AssertionError 。

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

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

containsOnly

Examples AssertJ Map Unit Test 驗證包含全部元素,順序不需要保持一致,若不成立,則會拋出 AssertionError 。

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

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

containsAllEntriesOf

Examples Unit Test AssertJ Map 驗證包含全部元素,大小可以小於,若不成立,則會拋出 AssertionError 。

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

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

containsExactly

Examples Unit Test AssertJ Map 驗證包含全部元素,順序也需要保持一致,若不成立,則會拋出 AssertionError 。

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

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

containsExactlyEntriesOf

Examples Unit Test AssertJ Map 驗證包含全部元素,順序也需要保持一致,若不成立,則會拋出 AssertionError 。

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

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

containsExactlyInAnyOrderEntriesOf

Examples Unit Test AssertJ Map 驗證包含全部元素,順序不需要保持一致,若不成立,則會拋出 AssertionError 錯誤, Unit Test AssertJ Map Example 提供範例參考。

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

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

UnitTestAssertJMapWithExamplesTest.java

Examples Unit Test AssertJ Map 新增單元測試,驗證 Unit Test AssertJ Map Example 是否符合預期。

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

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

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

import org.junit.jupiter.api.Test;

public class UnitTestAssertJMapWithExamplesTest {

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

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		assertThat(intMap).containsAnyOf(entry(9, 28), entry(1, 18), entry(3, 20));
	}
	
	@Test
	public void containsEntry() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		assertThat(map).containsEntry("Grape", 18);

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

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

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

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

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

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

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

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

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

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

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		Map<Integer, Integer> intMap2 = new HashMap<>();
		intMap2.put(1, 18);
		intMap2.put(2, 19);
		intMap2.put(3, 20);
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).containsExactlyEntriesOf(intMap2);
	}
	
	@Test
	public void containsExactlyInAnyOrderEntriesOf() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		Map<String, Integer> map2 = new LinkedHashMap<>();
		map2.put("Lemon", 20);
		map2.put("Kiwifruit", 19);
		map2.put("Grape", 18);
		System.out.println(map);
		System.out.println(map2);
		assertThat(map).containsExactlyInAnyOrderEntriesOf(map2);

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

心得分享

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

發佈留言