Testing with AssertJ Map in Java - AssertJ 155

Testing with AssertJ Map in Java – AssertJ 155

Testing with AssertJ Map in Java

通常與 JUnit 測試一起使用,被設計為在主流 IDE 中易於使用,讓開發者將繁瑣的測試驗證過程,變為更簡單,使用 Testing Java AssertJ Map 流式斷言,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

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

are

驗證元素是否符合條件判斷,若不成立,則會拋出 AssertionError 。

	@Test
	public void are() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 4, "length");
		assertThat(map.keySet()).are(length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 0, "value");
		assertThat(intMap.values()).are(intValue);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

areAtLeast

Java AssertJ Map Testing 驗證元素是否符合條件判斷,最少滿足幾個,若不成立,則會拋出 AssertionError 。

	@Test
	public void areAtLeast() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 5, "length");
		assertThat(map.keySet()).areAtLeast(1, length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 1, "value");
		assertThat(intMap.values()).areAtLeast(2, intValue);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

areAtLeastOne

Java AssertJ Map Testing 驗證元素是否符合條件判斷,最少滿足一個,若不成立,則會拋出 AssertionError 。

	@Test
	public void areAtLeastOne() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 5, "length");
		assertThat(map.keySet()).areAtLeastOne(length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 4, "value");
		assertThat(intMap.values()).areAtLeastOne(intValue);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

areAtMost

Java AssertJ Map Testing 驗證元素是否符合條件判斷,最多滿足幾個,若不成立,則會拋出 AssertionError 。

	@Test
	public void areAtMost() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 6, "length");
		assertThat(map.keySet()).areAtMost(1, length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 18, "value");
		assertThat(intMap.values()).areAtMost(2, intValue);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

areExactly

Java AssertJ Map Testing 驗證元素是否符合條件判斷,完全滿足幾個,若不成立,則會拋出 AssertionError 。

	@Test
	public void areExactly() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 5, "length");
		assertThat(map.keySet()).areExactly(1, length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 18, "value");
		assertThat(intMap.values()).areExactly(2, intValue);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

areNot

Java Testing AssertJ Map 驗證元素不符合條件判斷,若不成立,則會拋出 AssertionError 。

	@Test
	public void areNot() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 10, "length");
		assertThat(map.keySet()).areNot(length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 20, "value");
		assertThat(intMap.values()).areNot(intValue);
	}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}

as

Java Testing AssertJ Map 設定驗證描述,當驗證失敗時,顯示自定義訊息。

	@Test
	public void as() {
		int expectedSize = 2;
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		try {
			assertThat(map).as("%s's size should be equal to 2", "Map").isEqualTo(expectedSize);
		} catch (AssertionError e) {
			e.printStackTrace();
			assertThat(e).hasMessageContaining("size");
		}
	}
org.opentest4j.AssertionFailedError: [Map's size should be equal to 2] 
expected: 2
 but was: {"Grape"=18, "Kiwifruit"=19, "Lemon"=20}
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at org.ruoxue.spring_boot_168.test.assertj.map.TestingAssertJMapTest.as(TestingAssertJMapTest.java:136)

asWithSupplier

Java Testing AssertJ Map 設定驗證描述,當驗證失敗時,顯示自定義訊息, AssertJ Map Java Testing 提供範例參考。

	@Test
	public void asWithSupplier() {
		int expectedSize = 2;
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		String text = "Size expected: [" + expectedSize + "] but was: [" + map.size() + "]";
		try {
			Supplier<String> desc = () -> text;
			assertThat(map).as(desc).hasSize(expectedSize);
		} catch (AssertionError e) {
			e.printStackTrace();
			assertThat(e).hasMessageContaining(text);
		}
	}
java.lang.AssertionError: [Size expected: [2] but was: [3]] 
Expected size: 2 but was: 3 in:
{"Grape"=18, "Kiwifruit"=19, "Lemon"=20}
	at org.ruoxue.spring_boot_168.test.assertj.map.TestingAssertJMapTest.asWithSupplier(TestingAssertJMapTest.java:153)

TestingAssertJMapTest.java

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

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

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

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;

public class TestingAssertJMapTest {

	@Test
	public void are() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 4, "length");
		assertThat(map.keySet()).are(length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 0, "value");
		assertThat(intMap.values()).are(intValue);
	}

	@Test
	public void areAtLeast() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 5, "length");
		assertThat(map.keySet()).areAtLeast(1, length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 1, "value");
		assertThat(intMap.values()).areAtLeast(2, intValue);
	}

	@Test
	public void areAtLeastOne() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 5, "length");
		assertThat(map.keySet()).areAtLeastOne(length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 4, "value");
		assertThat(intMap.values()).areAtLeastOne(intValue);
	}

	@Test
	public void areAtMost() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 6, "length");
		assertThat(map.keySet()).areAtMost(1, length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 18, "value");
		assertThat(intMap.values()).areAtMost(2, intValue);
	}

	@Test
	public void areExactly() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 5, "length");
		assertThat(map.keySet()).areExactly(1, length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 18, "value");
		assertThat(intMap.values()).areExactly(2, intValue);
	}

	@Test
	public void areNot() {
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		System.out.println(map);
		Condition<String> length = new Condition<String>(s -> s.length() > 10, "length");
		assertThat(map.keySet()).areNot(length);

		Map<Integer, Integer> intMap = new HashMap<>();
		intMap.put(1, 18);
		intMap.put(2, 19);
		intMap.put(3, 20);
		System.out.println(intMap);
		Condition<Integer> intValue = new Condition<Integer>(i -> i > 20, "value");
		assertThat(intMap.values()).areNot(intValue);
	}

	@Test
	public void as() {
		int expectedSize = 2;
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		try {
			assertThat(map).as("%s's size should be equal to 2", "Map").isEqualTo(expectedSize);
		} catch (AssertionError e) {
			e.printStackTrace();
			assertThat(e).hasMessageContaining("size");
		}
	}

	@Test
	public void asWithSupplier() {
		int expectedSize = 2;
		Map<String, Integer> map = new HashMap<>();
		map.put("Grape", 18);
		map.put("Kiwifruit", 19);
		map.put("Lemon", 20);
		String text = "Size expected: [" + expectedSize + "] but was: [" + map.size() + "]";
		try {
			Supplier<String> desc = () -> text;
			assertThat(map).as(desc).hasSize(expectedSize);
		} catch (AssertionError e) {
			e.printStackTrace();
			assertThat(e).hasMessageContaining(text);
		}
	}
}

心得分享

AssertJ Map Java Testing 提供驗證元素條件判斷等方法,編寫的測試程式使用流暢的斷言語句,通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,善用 Java Testing AssertJ Map 將有助於驗證效率的提升。

發佈留言