AssertJ Map Methods - AssertJ 155

AssertJ Map Methods – AssertJ 155

AssertJ Map Methods

這些斷言語句通常與 JUnit 測試一起使用,為用 Java 編寫的測試程式提供了流暢的斷言語句, 斷言的基本方法是 assertThat 方法,善用 AssertJ Map Java Methods 將有助於驗證效率的提升,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

AssertJ Map Methods Java 斷言集合的主要目的是取得集合的正確元素以進行斷言。

isNull

驗證是否為空值,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNull() {
		Map<String, Integer> map = null;
		System.out.println(map);
		assertThat(map).isNull();

		Map<Integer, Integer> intMap = null;
		System.out.println(intMap);
		assertThat(intMap).isNull();
	}
null
null

isNotNull

AssertJ Map Methods Java 驗證是否不為空值,若不成立,則會拋出 AssertionError 。

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

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

isEmpty

AssertJ Map Methods Java 驗證是否為空集合,若不成立,則會拋出 AssertionError 。

	@Test
	public void isEmpty() {
		Map<String, Integer> map = new HashMap<>();
		System.out.println(map);
		assertThat(map).isEmpty();

		Map<Integer, Integer> intMap = new HashMap<>();
		System.out.println(intMap);
		assertThat(intMap).isEmpty();
	}
[]
[]

isNotEmpty

AssertJ Map Methods Java 驗證是否不為空集合,若不成立,則會拋出 AssertionError 。

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

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

isNullOrEmpty

AssertJ Map Methods Java 驗證是否為空值或為空集合,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNullOrEmpty() {
		Map<String, Integer> map = null;
		System.out.println(map);
		map = new HashMap<>();
		System.out.println(map);
		assertThat(map).isNullOrEmpty();

		Map<Integer, Integer> intMap = null;
		System.out.println(intMap);
		intMap = new HashMap<>();
		System.out.println(intMap);
		assertThat(intMap).isNullOrEmpty();
	}
null
[]
null
[]

isEqualTo

AssertJ Map Methods in Java 驗證元素是否相等,若不成立,則會拋出 AssertionError 。

	@Test
	public void isEqualTo() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		Map<String, Integer> map2 = new HashMap<>(map);
		System.out.println(map);
		System.out.println(map2);
		assertThat(map).isEqualTo(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<>(intMap);
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).isEqualTo(intMap2);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{Kiwifruit=19, Lemon=20, Grape=18}
{1=18, 2=19, 3=20}
{1=18, 2=19, 3=20}

isNotEqualTo

AssertJ Map Methods in Java 驗證元素是否不相等,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotEqualTo() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		Map<String, Integer> map2 = new HashMap<>();
		System.out.println(map);
		System.out.println(map2);
		assertThat(map).isNotEqualTo(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<>();
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).isNotEqualTo(intMap2);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{}
{1=18, 2=19, 3=20}
{}

isSameAs

AssertJ Map Functions in Java 驗證實例是否相等,若不成立,則會拋出 AssertionError 。

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

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		Map<Integer, Integer> intMap2 = intMap;
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).isSameAs(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}

isNotSameAs

AssertJ Map Functions in Java 驗證實例是否不相等,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotSameAs() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		Map<String, Integer> map2 = new HashMap<>(map);
		System.out.println(map);
		System.out.println(map2);
		assertThat(map).isNotSameAs(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<>(intMap);
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).isNotSameAs(intMap2);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{Kiwifruit=19, Lemon=20, Grape=18}
{1=18, 2=19, 3=20}
{1=18, 2=19, 3=20}

AssertJMapMethodsTest.java

AssertJ Map Methods in Java 新增單元測試,驗證是否符合預期。

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.junit.jupiter.api.Test;

public class AssertJMapMethodsTest {

	@Test
	public void isNull() {
		Map<String, Integer> map = null;
		System.out.println(map);
		assertThat(map).isNull();

		Map<Integer, Integer> intMap = null;
		System.out.println(intMap);
		assertThat(intMap).isNull();
	}

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

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

	@Test
	public void isEmpty() {
		Map<String, Integer> map = new HashMap<>();
		System.out.println(map);
		assertThat(map).isEmpty();

		Map<Integer, Integer> intMap = new HashMap<>();
		System.out.println(intMap);
		assertThat(intMap).isEmpty();
	}

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

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

	@Test
	public void isNullOrEmpty() {
		Map<String, Integer> map = null;
		System.out.println(map);
		map = new HashMap<>();
		System.out.println(map);
		assertThat(map).isNullOrEmpty();

		Map<Integer, Integer> intMap = null;
		System.out.println(intMap);
		intMap = new HashMap<>();
		System.out.println(intMap);
		assertThat(intMap).isNullOrEmpty();
	}

	@Test
	public void isEqualTo() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		Map<String, Integer> map2 = new HashMap<>(map);
		System.out.println(map);
		System.out.println(map2);
		assertThat(map).isEqualTo(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<>(intMap);
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).isEqualTo(intMap2);
	}

	@Test
	public void isNotEqualTo() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		Map<String, Integer> map2 = new HashMap<>();
		System.out.println(map);
		System.out.println(map2);
		assertThat(map).isNotEqualTo(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<>();
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).isNotEqualTo(intMap2);
	}

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

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

	@Test
	public void isNotSameAs() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		Map<String, Integer> map2 = new HashMap<>(map);
		System.out.println(map);
		System.out.println(map2);
		assertThat(map).isNotSameAs(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<>(intMap);
		System.out.println(intMap);
		System.out.println(intMap2);
		assertThat(intMap).isNotSameAs(intMap2);
	}
}

心得分享

AssertJ Map Functions in Java 是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,驗證 Map 是否為空值、空集合、指定類別實例、元素相等, 提高了測試程式碼的可讀性,使用 AssertJ Map Methods in Java 流式斷言,提高了測試程式碼的可讀性。

發佈留言