Table of Contents
ToggleAssertJ Map in Java with Examples
編寫斷言的套件庫,AssertJ 為編寫斷言,提供了流暢的寫法,類似的流暢或鍊式寫法,並且語法跟自然語言相近,對於編寫測試時,力求容易閱讀及維護這上提供了相當大的改進, AssertJ Map in Java 驗證 Map 是否相同元素、大小等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- map
| +- AssertJMapWithExamplesTest.java
單元測試
AssertJ Map Java 斷言集合的主要目的是取得集合的正確元素以進行斷言。
has
驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void has() {
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
Condition<Map<?, ?>> size = new Condition<Map<?, ?>>(s -> s.size() > 2, "size");
assertThat(map).has(size);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
Condition<Map<?, ?>> intSize = new Condition<Map<?, ?>>(i -> i.size() > 2, "size");
assertThat(intMap).has(intSize);
}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}
hasSize
AssertJ Map Java 驗證大小是否為指定值,若不成立,則會拋出 AssertionError 。
@Test
public void hasSize() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSize(expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSize(expectedSize);
}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}
hasSizeBetween
AssertJ Map Java 驗證大小是否介於起始值之間,若不成立,則會拋出 AssertionError 。
@Test
public void hasSizeBetween() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSizeBetween(1, expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSizeBetween(1, expectedSize);
}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}
hasSizeGreaterThan
AssertJ Map Java 驗證大小是否大於指定值,若不成立,則會拋出 AssertionError 。
@Test
public void hasSizeGreaterThan() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSizeGreaterThan(expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSizeGreaterThan(expectedSize);
}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}
hasSizeLessThan
Java AssertJ Map 驗證大小是否小於指定值,若不成立,則會拋出 AssertionError 。
@Test
public void hasSizeLessThan() {
int expectedSize = 4;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSizeLessThan(expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSizeLessThan(expectedSize);
}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}
hasToString
Java AssertJ Map 驗證 toString 是否相等,若不成立,則會拋出 AssertionError 。
@Test
public void hasToString() {
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasToString(map.toString());
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasToString(intMap.toString());
}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}
hasSameSizeAs
Java AssertJ Map 驗證大小是否相等,若不成立,則會拋出 AssertionError 。
@Test
public void hasSameSizeAs() {
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).hasSameSizeAs(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).hasSameSizeAs(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}
doesNotHave
Java AssertJ Map Example 驗證是否不符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHave() {
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
Condition<Map<?, ?>> size = new Condition<Map<?, ?>>(s -> s.size() > 3, "size");
assertThat(map).doesNotHave(size);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
Condition<Map<?, ?>> intSize = new Condition<Map<?, ?>>(i -> i.size() > 3, "size");
assertThat(intMap).doesNotHave(intSize);
}
{Grape=18, Kiwifruit=19, Lemon=20}
{1=18, 2=19, 3=20}
AssertJMapWithExamplesTest.java
Java AssertJ Map 新增單元測試,驗證 Java 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.Map;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
public class AssertJMapWithExamplesTest {
@Test
public void has() {
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
Condition<Map<?, ?>> size = new Condition<Map<?, ?>>(s -> s.size() > 2, "size");
assertThat(map).has(size);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
Condition<Map<?, ?>> intSize = new Condition<Map<?, ?>>(i -> i.size() > 2, "size");
assertThat(intMap).has(intSize);
}
@Test
public void hasSize() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSize(expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSize(expectedSize);
}
@Test
public void hasSizeBetween() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSizeBetween(1, expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSizeBetween(1, expectedSize);
}
@Test
public void hasSizeGreaterThan() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSizeGreaterThan(expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSizeGreaterThan(expectedSize);
}
@Test
public void hasSizeLessThan() {
int expectedSize = 4;
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasSizeLessThan(expectedSize);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasSizeLessThan(expectedSize);
}
@Test
public void hasToString() {
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
assertThat(map).hasToString(map.toString());
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
assertThat(intMap).hasToString(intMap.toString());
}
@Test
public void hasSameSizeAs() {
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).hasSameSizeAs(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).hasSameSizeAs(intMap2);
}
@Test
public void doesNotHave() {
Map<String, Integer> map = new HashMap<>();
map.put("Grape", 18);
map.put("Kiwifruit", 19);
map.put("Lemon", 20);
System.out.println(map);
Condition<Map<?, ?>> size = new Condition<Map<?, ?>>(s -> s.size() > 3, "size");
assertThat(map).doesNotHave(size);
Map<Integer, Integer> intMap = new HashMap<>();
intMap.put(1, 18);
intMap.put(2, 19);
intMap.put(3, 20);
System.out.println(intMap);
Condition<Map<?, ?>> intSize = new Condition<Map<?, ?>>(i -> i.size() > 3, "size");
assertThat(intMap).doesNotHave(intSize);
}
}
心得分享
Java AssertJ Map Example 為用 Java 編寫的測試程式提供了流暢的斷言語句,這些斷言語句通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,驗證 Map 是否相同元素、大小,善用 Java AssertJ Map 將有助於驗證效率的提升。