AssertJ List Assertions - AssertJ 155

AssertJ List Assertions – AssertJ 155

AssertJ List Assertions

驗證包含、不包含指定的元素等, Asserting List with AssertJ 使用流式斷言,可以大幅提升斷言效率,提高了測試程式碼的可讀性,本篇增加了範例,透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- spring_boot_168
       |               +- test
       |                   +- assertj
       |                       +- list
       |                           +- AssertJListAssertionsTest.java   

單元測試

List Elements with AssertJ 斷言列表的主要目的是取得列表的正確元素以進行斷言。

contains

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

	@Test
	public void contains() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).contains("Banana", "Apple");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).contains(6, 7, 8);
	}
[Apple, Banana, Cherry]
[6, 7, 8, 9, 10]

doesNotContain

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

	@Test
	public void doesNotContain() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).doesNotContain("Grape");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).doesNotContain(1);
	}
[Apple, Banana, Cherry]
[6, 7, 8, 9, 10]

containsOnly

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

	@Test
	public void containsOnly() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsOnly("Banana", "Cherry", "Apple");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsOnly(10, 9, 8, 7, 6);
	}
[Apple, Banana, Cherry]
[6, 7, 8, 9, 10]

containsOnlyOnce

List Elements with AssertJ 驗證包含只出現一次的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsOnlyOnce() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsOnlyOnce("Banana");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsOnlyOnce(8);
	}
[Apple, Banana, Cherry]
[6, 7, 8, 9, 10]

containsOnlyNulls

List Elements with AssertJ 驗證只包含空值,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsOnlyNulls() {
		List<String> list = Arrays.asList(null, null, null);
		System.out.println(list);
		assertThat(list).containsOnlyNulls();

		List<Integer> intList = Arrays.asList(null, null, null, null, null);
		System.out.println(intList);
		assertThat(intList).containsOnlyNulls();
	}
[null, null, null]
[null, null, null, null, null]

containsAll

List Elements with AssertJ 驗證包含全部元素,大小可以小於,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsAll() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		List<String> list2 = Arrays.asList("Apple", "Banana");
		System.out.println(list);
		System.out.println(list2);
		assertThat(list).containsAll(list2);

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		List<Integer> intList2 = Arrays.asList(6, 7, 8);
		System.out.println(intList);
		System.out.println(intList2);
		assertThat(intList).containsAll(intList2);
	}
[Apple, Banana, Cherry]
[Apple, Banana]
[6, 7, 8, 9, 10]
[6, 7, 8]

containsExactly

Testing Java List with AssertJ 驗證包含全部元素,順序也需要保持一致,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsExactly() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsExactly("Apple", "Banana", "Cherry");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsExactly(6, 7, 8, 9, 10);
	}
[Apple, Banana, Cherry]
[6, 7, 8, 9, 10]

containsExactlyInAnyOrder

Testing Java List with AssertJ 驗驗證包含全部元素,順序不需要保持一致,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsExactlyInAnyOrder() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsExactlyInAnyOrder("Cherry", "Apple", "Banana");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsExactlyInAnyOrder(9, 10, 6, 7, 8);
	}
[Apple, Banana, Cherry]
[6, 7, 8, 9, 10]

AssertJListAssertionsTest.java

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

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

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

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

public class AssertJListAssertionsTest {

	@Test
	public void contains() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).contains("Banana", "Apple");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).contains(6, 7, 8);
	}

	@Test
	public void doesNotContain() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).doesNotContain("Grape");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).doesNotContain(1);
	}

	@Test
	public void containsOnly() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsOnly("Banana", "Cherry", "Apple");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsOnly(10, 9, 8, 7, 6);
	}

	@Test
	public void containsOnlyOnce() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsOnlyOnce("Banana");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsOnlyOnce(8);
	}

	@Test
	public void containsOnlyNulls() {
		List<String> list = Arrays.asList(null, null, null);
		System.out.println(list);
		assertThat(list).containsOnlyNulls();

		List<Integer> intList = Arrays.asList(null, null, null, null, null);
		System.out.println(intList);
		assertThat(intList).containsOnlyNulls();
	}

	@Test
	public void containsAll() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		List<String> list2 = Arrays.asList("Apple", "Banana");
		System.out.println(list);
		System.out.println(list2);
		assertThat(list).containsAll(list2);

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		List<Integer> intList2 = Arrays.asList(6, 7, 8);
		System.out.println(intList);
		System.out.println(intList2);
		assertThat(intList).containsAll(intList2);
	}

	@Test
	public void containsExactly() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsExactly("Apple", "Banana", "Cherry");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsExactly(6, 7, 8, 9, 10);
	}

	@Test
	public void containsExactlyInAnyOrder() {
		List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
		System.out.println(list);
		assertThat(list).containsExactlyInAnyOrder("Cherry", "Apple", "Banana");

		List<Integer> intList = Stream.of(6, 7, 8, 9, 10).collect(Collectors.toList());
		System.out.println(intList);
		assertThat(intList).containsExactlyInAnyOrder(9, 10, 6, 7, 8);
	}
}

心得分享

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

發佈留言