Asserting Arrays with AssertJ - AssertJ 155

Asserting Arrays with AssertJ – AssertJ 155

Asserting Arrays with AssertJ

是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,提高了測試程式碼的可讀性,驗證 Array 過濾滿足條件的元素,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Assert Arrays in Java 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- spring_boot_168
       |               +- test
       |                   +- assertj
       |                       +- array
       |                           +- AssertingArraysTest.java   

單元測試

Assertions Arrays in Java 斷言陣列的主要目的是取得陣列的正確元素以進行斷言。

Fruit

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

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	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();
		}
	}

filteredOn

Assertions Arrays in Java 使用 Function , 驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void filteredOn() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn(Fruit::getName, "Guava").hasSize(expectedSize);
		assertThat(array).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
	}
[{"name":"Durian","quantity":1.7976931348623157E308,"type":1}, {"name":"Guava","quantity":1.0,"type":2}, {"name":"Pitaya","quantity":-1.0,"type":3}]

filteredOnWithPredicate

Assertions Arrays in Java 使用 Predicate , 驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void filteredOnWithPredicate() {
		int expectedSize = 2;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn(e -> e.getName().length() > 5).hasSize(expectedSize);
		assertThat(array).filteredOn(e -> e.getQuantity() > 0).hasSize(expectedSize);
	}
[{"name":"Durian","quantity":1.7976931348623157E308,"type":1}, {"name":"Guava","quantity":1.0,"type":2}, {"name":"Pitaya","quantity":-1.0,"type":3}]

filteredOnWithFieldName

Assertions Arrays in Java 使用 field name , 驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void filteredOnWithFieldName() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn("name", "Guava").hasSize(expectedSize);
		assertThat(array).filteredOn("type", 2).hasSize(expectedSize);
	}
[{"name":"Durian","quantity":1.7976931348623157E308,"type":1}, {"name":"Guava","quantity":1.0,"type":2}, {"name":"Pitaya","quantity":-1.0,"type":3}]

filteredOnWithFilterOperator

Asserting Arrays with Examples 使用 FilterOperator , 驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void filteredOnWithFilterOperator() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn("name", in("Guava", "Pitaya")).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
		assertThat(array).filteredOn("type", notIn(1, 2)).filteredOn(e -> e.getQuantity() < 0).hasSize(expectedSize);
	}
[{"name":"Durian","quantity":1.7976931348623157E308,"type":1}, {"name":"Guava","quantity":1.0,"type":2}, {"name":"Pitaya","quantity":-1.0,"type":3}]

filteredOnWithCondition

Asserting Arrays with Examples 使用 Condition ,驗證符合過濾條件的元素,若不成立,則會拋出 AssertionError 。

	@Test
	public void filteredOnWithCondition() {
		int expectedSize = 2;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		Condition<Fruit> length = new Condition<Fruit>(o -> o.name.length() > 5, "length");
		assertThat(array).filteredOn(length).hasSize(expectedSize);
		
		Condition<Fruit> quantity = new Condition<Fruit>(o -> o.quantity > 0, "quantity");
		assertThat(array).filteredOn(quantity).hasSize(expectedSize);
	}
[{"name":"Durian","quantity":1.7976931348623157E308,"type":1}, {"name":"Guava","quantity":1.0,"type":2}, {"name":"Pitaya","quantity":-1.0,"type":3}]

filteredOnAssertions

Asserting Arrays with Examples 使用 ThrowingConsumer 驗證邏輯時,發生錯誤,拋出例外

	@Test
	public void filteredOnAssertions() {
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit(null, 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOnAssertions(e -> {
			double result = 1 / 0;
		});
	}
java.lang.ArithmeticException: / by zero
	at org.ruoxue.spring_boot_168.test.assertj.array.AssertingArraysTest.lambda$8(AssertingArraysTest.java:114)
	at org.assertj.core.api.ThrowingConsumer.accept(ThrowingConsumer.java:30)

filteredOnNull

驗證符合空值的元素屬性,若不成立,則會拋出 AssertionError 。

	@Test
	public void filteredOnNull() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit(null, 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOnNull("name").hasSize(expectedSize);
	}
[{"name":"Durian","quantity":1.7976931348623157E308,"type":1}, {"name":null,"quantity":1.0,"type":2}, {"name":"Pitaya","quantity":-1.0,"type":3}]

AssertingArraysTest.java

Asserting Arrays with Examples 新增單元測試,驗證是否符合預期。

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

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

import java.util.Arrays;

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

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

public class AssertingArraysTest {

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	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 filteredOn() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn(Fruit::getName, "Guava").hasSize(expectedSize);
		assertThat(array).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
	}

	@Test
	public void filteredOnWithPredicate() {
		int expectedSize = 2;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn(e -> e.getName().length() > 5).hasSize(expectedSize);
		assertThat(array).filteredOn(e -> e.getQuantity() > 0).hasSize(expectedSize);
	}

	@Test
	public void filteredOnWithFieldName() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn("name", "Guava").hasSize(expectedSize);
		assertThat(array).filteredOn("type", 2).hasSize(expectedSize);
	}

	@Test
	public void filteredOnWithFilterOperator() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOn("name", in("Guava", "Pitaya")).filteredOn(Fruit::getType, 2).hasSize(expectedSize);
		assertThat(array).filteredOn("type", notIn(1, 2)).filteredOn(e -> e.getQuantity() < 0).hasSize(expectedSize);
	}

	@Test
	public void filteredOnWithCondition() {
		int expectedSize = 2;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit("Guava", 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		Condition<Fruit> length = new Condition<Fruit>(o -> o.name.length() > 5, "length");
		assertThat(array).filteredOn(length).hasSize(expectedSize);

		Condition<Fruit> quantity = new Condition<Fruit>(o -> o.quantity > 0, "quantity");
		assertThat(array).filteredOn(quantity).hasSize(expectedSize);
	}

	@Test
	public void filteredOnAssertions() {
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit(null, 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		assertThat(array).filteredOnAssertions(e -> {
			double result = 1 / 0;
		});
	}

	@Test
	public void filteredOnNull() {
		int expectedSize = 1;
		Fruit durian = new Fruit("Durian", Double.MAX_VALUE, 1);
		Fruit guava = new Fruit(null, 1, 2);
		Fruit pitaya = new Fruit("Pitaya", -1, 3);
		Fruit[] array = new Fruit[] { durian, guava, pitaya };
		System.out.println(Arrays.deepToString(array));
		assertThat(array).filteredOnNull("name").hasSize(expectedSize);
	}
}

心得分享

Testing Java Arrays with AssertJ 除了提供流式判斷,還針對 Array 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈, Asserting Arrays with Examples 介紹 filteredOn 等方法,提供範例參考。

發佈留言