AssertJ Optional Methods - AssertJ 155

AssertJ Optional Methods – AssertJ 155

AssertJ Optional Methods

驗證 Optional 是否為空值、相同實例、相等方法, AssertJ 是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,使用 AssertJ Optional Java Methods 流式斷言,提高了測試程式碼的可讀性,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- spring_boot_168
       |               +- test
       |                   +- assertj
       |                       +- optional
       |                           +- AssertJOptionalMethodsTest.java   

單元測試

AssertJ Optional Methods Java 斷言 Optional 的主要目的是取得 Optional 以進行斷言。

isNull

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

	@Test
	public void isNull() {
		Optional<String> value = null;
		System.out.println(value);
		assertThat(value).isNull();

		Optional<Integer> intValue = null;
		System.out.println(intValue);
		assertThat(intValue).isNull();
	}
null
null

isNotNull

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

	@Test
	public void isNotNull() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isNotNull();

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isNotNull();
	}
Optional[AssertJ]
Optional[155]

isEmpty

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

	@Test
	public void isEmpty() {
		Optional<String> value = Optional.empty();
		System.out.println(value);
		assertThat(value).isEmpty();

		Optional<Integer> intValue = Optional.empty();
		System.out.println(intValue);
		assertThat(intValue).isEmpty();
	}
Optional.empty
Optional.empty

isNotEmpty

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

	@Test
	public void isNotEmpty() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isNotEmpty();

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isNotEmpty();
	}
Optional[AssertJ]
Optional[155]

isEqualTo

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

	@Test
	public void isEqualTo() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isEqualTo(Optional.of("AssertJ"));

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isEqualTo(Optional.of(155));
	}
Optional[AssertJ]
Optional[155]

isNotEqualTo

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

	@Test
	public void isNotEqualTo() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isNotEqualTo(Optional.empty());

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isNotEqualTo(Optional.empty());
	}
Optional[AssertJ]
Optional[155]

isSameAs

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

	@Test
	public void isSameAs() {
		Optional<String> value = Optional.of("AssertJ");
		Optional<String> value2 = value;
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isSameAs(value2);

		Optional<Integer> intValue = Optional.of(155);
		Optional<Integer> intValue2 = intValue;
		System.out.println(intValue);
		System.out.println(intValue2);
		assertThat(intValue).isSameAs(intValue2);
	}
Optional[AssertJ]
Optional[AssertJ]
Optional[155]
Optional[155]

isNotSameAs

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

	@Test
	public void isNotSameAs() {
		Optional<String> value = Optional.of("AssertJ");
		Optional<String> value2 = Optional.of("AssertJ");
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isNotSameAs(value2);

		Optional<Integer> intValue = Optional.of(155);
		Optional<Integer> intValue2 = Optional.of(155);
		System.out.println(intValue);
		System.out.println(intValue2);
		assertThat(intValue).isNotSameAs(intValue2);
	}
Optional[AssertJ]
Optional[AssertJ]
Optional[155]
Optional[155]

AssertJOptionalMethodsTest.java

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

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

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

import java.util.Optional;

import org.junit.jupiter.api.Test;

public class AssertJOptionalMethodsTest {

	@Test
	public void isNull() {
		Optional<String> value = null;
		System.out.println(value);
		assertThat(value).isNull();

		Optional<Integer> intValue = null;
		System.out.println(intValue);
		assertThat(intValue).isNull();
	}

	@Test
	public void isNotNull() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isNotNull();

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isNotNull();
	}

	@Test
	public void isEmpty() {
		Optional<String> value = Optional.empty();
		System.out.println(value);
		assertThat(value).isEmpty();

		Optional<Integer> intValue = Optional.empty();
		System.out.println(intValue);
		assertThat(intValue).isEmpty();
	}

	@Test
	public void isNotEmpty() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isNotEmpty();

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isNotEmpty();
	}

	@Test
	public void isEqualTo() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isEqualTo(Optional.of("AssertJ"));

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isEqualTo(Optional.of(155));
	}

	@Test
	public void isNotEqualTo() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		assertThat(value).isNotEqualTo(Optional.empty());

		Optional<Integer> intValue = Optional.of(155);
		System.out.println(intValue);
		assertThat(intValue).isNotEqualTo(Optional.empty());
	}

	@Test
	public void isSameAs() {
		Optional<String> value = Optional.of("AssertJ");
		Optional<String> value2 = value;
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isSameAs(value2);

		Optional<Integer> intValue = Optional.of(155);
		Optional<Integer> intValue2 = intValue;
		System.out.println(intValue);
		System.out.println(intValue2);
		assertThat(intValue).isSameAs(intValue2);
	}

	@Test
	public void isNotSameAs() {
		Optional<String> value = Optional.of("AssertJ");
		Optional<String> value2 = Optional.of("AssertJ");
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isNotSameAs(value2);

		Optional<Integer> intValue = Optional.of(155);
		Optional<Integer> intValue2 = Optional.of(155);
		System.out.println(intValue);
		System.out.println(intValue2);
		assertThat(intValue).isNotSameAs(intValue2);
	}
}

心得分享

AssertJ Optional Functions in Java 為用 Java 編寫的測試程式提供了流暢的斷言語句,這些斷言語句通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,善用 AssertJ Optional Methods in Java 將有助於驗證效率的提升。

發佈留言