AssertJ Optional Assertions - AssertJ 155

AssertJ Optional Assertions – AssertJ 155

AssertJ Optional Assertions

提供驗證存在、不存在、包含指定的值等方法, AssertJ 是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Asserting Optional with AssertJ 使用流式斷言,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

Optional Elements with AssertJ 斷言 Optional 的主要目的是取得 Optional 以進行斷言。

isPresent

驗證 Optional 容器內值是否存在,若不成立,則會拋出 AssertionError 。

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

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

isNotPresent

Optional Elements with AssertJ 驗證 Optional 容器內值是否不存在,若不成立,則會拋出 AssertionError 。

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

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

contains

Optional Elements with AssertJ 驗證 Optional 容器內包含指定的值,若不成立,則會拋出 AssertionError 。

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

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

containsInstanceOf

Optional Elements with AssertJ 驗證 Optional 容器內包含指定的實例,若不成立,則會拋出 AssertionError 。

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

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

containsSame

Optional Elements with AssertJ 驗證 Optional 容器內值實例是否相等,若不成立,則會拋出 AssertionError 。

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

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

containsThrowError

Testing Java Optional with AssertJ 驗證 Optional 容器內包含指定的值,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsThrowError() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		try {
			assertThat(value).contains("155");
		} catch (AssertionError e) {
			e.printStackTrace();
		}
	}
Optional[AssertJ]

org.opentest4j.AssertionFailedError: 
Expecting actual:
  Optional[AssertJ]
to contain:
  "155"
but did not.
	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.optional.AssertJOptionalAssertionsTest.containsThrowError(AssertJOptionalAssertionsTest.java:73)

containsThrownBy

Testing Java Optional with AssertJ 驗證 Optional 容器內包含指定的值,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsThrownBy() {
		assertThatThrownBy(() -> {
			Optional<String> value = Optional.of("AssertJ");
			System.out.println(value);
			assertThat(value).contains("155");
		}).isInstanceOf(AssertionError.class);
	}
Optional[AssertJ]

AssertJOptionalAssertionsTest.java

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

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 AssertJOptionalAssertionsTest {

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

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

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

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

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

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

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

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

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

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

	@Test
	public void containsThrowError() {
		Optional<String> value = Optional.of("AssertJ");
		System.out.println(value);
		try {
			assertThat(value).contains("155");
		} catch (AssertionError e) {
			e.printStackTrace();
		}
	}

	@Test
	public void containsThrownBy() {
		assertThatThrownBy(() -> {
			Optional<String> value = Optional.of("AssertJ");
			System.out.println(value);
			assertThat(value).contains("155");
		}).isInstanceOf(AssertionError.class);
	}
}

心得分享

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

發佈留言