AssertJ Object Assertions - AssertJ 155

AssertJ Object Assertions – AssertJ 155

AssertJ Object Assertions

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

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- spring_boot_168
       |               +- test
       |                   +- assertj
       |                       +- object
       |                           +- AssertJObjectAssertionsTest.java   

單元測試

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

Fruit

建立 Fruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。

	@NoArgsConstructor
	@Getter
	@Setter
	public static class Fruit {
		private String name;
		private double quantity;
		private int type;

		public Fruit(String name, double quantity, int type) {
			this.name = name;
			this.quantity = quantity;
			this.type = type;
		}

		public String toString() {
			ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
			builder.appendSuper(super.toString());
			builder.append("name", name);
			builder.append("quantity", quantity);
			builder.append("type", type);
			return builder.toString();
		}
	}

contains

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

	@Test
	public void contains() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).contains("Litchi", "101");
		assertThat(value).extracting("name").asInstanceOf(STRING).contains("chi", "1");
	}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}

doesNotContain

Object Elements with AssertJ 驗證 不包含指定的物件,若不成立,則會拋出 AssertionError 。

	@Test
	public void doesNotContain() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContain("Pomelo", "99");
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContain("elo", "9");
	}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}

containsOnlyOnce

Object Elements with AssertJ 驗證包含只出現一次的物件,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsOnlyOnce() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyOnce("chi");
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyOnce("01");
	}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}

containsOnlyWhitespaces

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

	@Test
	public void containsOnlyWhitespaces() {
		Object value = new Fruit(" ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyWhitespaces();
		value = new Fruit("   ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyWhitespaces();
	}
{"name":" ","quantity":1.7976931348623157E308,"type":1}
{"name":"   ","quantity":1.7976931348623157E308,"type":1} 

doesNotContainOnlyWhitespaces

Testing Java Object with AssertJ 驗證不只包含空白,若不成立,則會拋出 AssertionError 。

	@Test
	public void doesNotContainOnlyWhitespaces() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainOnlyWhitespaces();
		value = new Fruit("Litchi ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainOnlyWhitespaces();
	}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
{"name":"Litchi ","quantity":1.7976931348623157E308,"type":1}

containsOnlyDigits

Testing Java Object with AssertJ 驗證只包含數字,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsOnlyDigits() {
		Object value = new Fruit("101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyDigits();
		value = new Fruit("1", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyDigits();
	}
{"name":"101","quantity":1.7976931348623157E308,"type":1}
{"name":"1","quantity":1.7976931348623157E308,"type":1}

containsWhitespaces

Testing Java Object with AssertJ 驗證包含空白,若不成立,則會拋出 AssertionError 。

	@Test
	public void containsWhitespaces() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsWhitespaces();
		value = new Fruit(" ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsWhitespaces();
	}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
{"name":" ","quantity":1.7976931348623157E308,"type":1}

doesNotContainAnyWhitespaces

Testing Java Object with AssertJ 驗證不包含任何空白,若不成立,則會拋出 AssertionError 。

	@Test
	public void doesNotContainAnyWhitespaces() {
		Object value = new Fruit("Litchi101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainAnyWhitespaces();
		value = new Fruit("Litchi", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainAnyWhitespaces();
	}
{"name":"Litchi101","quantity":1.7976931348623157E308,"type":1}
{"name":"Litchi","quantity":1.7976931348623157E308,"type":1}

AssertJObjectAssertionsTest.java

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

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

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

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.jupiter.api.Test;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class AssertJObjectAssertionsTest {

	@NoArgsConstructor
	@Getter
	@Setter
	public static class Fruit {
		private String name;
		private double quantity;
		private int type;

		public Fruit(String name, double quantity, int type) {
			this.name = name;
			this.quantity = quantity;
			this.type = type;
		}

		public String toString() {
			ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
			builder.appendSuper(super.toString());
			builder.append("name", name);
			builder.append("quantity", quantity);
			builder.append("type", type);
			return builder.toString();
		}
	}

	@Test
	public void contains() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).contains("Litchi", "101");
		assertThat(value).extracting("name").asInstanceOf(STRING).contains("chi", "1");
	}

	@Test
	public void doesNotContain() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContain("Pomelo", "99");
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContain("elo", "9");
	}

	@Test
	public void containsOnlyOnce() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyOnce("chi");
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyOnce("01");
	}

	@Test
	public void containsOnlyWhitespaces() {
		Object value = new Fruit(" ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyWhitespaces();
		value = new Fruit("   ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyWhitespaces();
	}

	@Test
	public void doesNotContainOnlyWhitespaces() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainOnlyWhitespaces();
		value = new Fruit("Litchi ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainOnlyWhitespaces();
	}

	@Test
	public void containsOnlyDigits() {
		Object value = new Fruit("101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyDigits();
		value = new Fruit("1", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsOnlyDigits();
	}

	@Test
	public void containsWhitespaces() {
		Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsWhitespaces();
		value = new Fruit(" ", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).containsWhitespaces();
	}

	@Test
	public void doesNotContainAnyWhitespaces() {
		Object value = new Fruit("Litchi101", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainAnyWhitespaces();
		value = new Fruit("Litchi", Double.MAX_VALUE, 1);
		System.out.println(value);
		assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainAnyWhitespaces();
	}
}

心得分享

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

發佈留言