AssertJ Java 8 Features - AssertJ 155

AssertJ Java 8 Features – AssertJ 155

  • Post author:
  • Post category:RD / AssertJ
  • Post comments:0 Comments
  • Post last modified:2023-09-18

AssertJ Java 8 Features

介紹 AssertJ 的 Java 8 相關功能,為 Java 8 類型提供特殊的輔助方法,及新斷言使用 Java 8 功能,通過 Java 8 Features AssertJ 流式斷言,可以大幅提升斷言效率,減少程式碼的撰寫,讓開發者體驗更流暢的驗證斷言,本篇增加了範例,透過單元測試來驗證產出結果。

檔案目錄

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

Dependencies

從 AssertJ 3.5.1 版本開始,就已經支援 Java 8 ,因此須將引用其程式庫。
Java 7 或是更早之前的版本,則須引用 AssertJ 2.x.x。

Gradle

build.gradle

dependencies {
	testImplementation "org.assertj:assertj-core:3.23.1"
}

Maven

pom.xml

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.23.1</version>
    <scope>test</scope>
</dependency>

單元測試

Assertions Java 8 Features 斷言物件的主要目的是取得物件以進行斷言。

Fruit

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

	@NoArgsConstructor
	@Getter
	@Setter
	public static class Fruit {
		private String name;
		private double quantity;
		private int type;
		private List<String> origins = new ArrayList<>();

		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);
			builder.append("origins", origins);
			return builder.toString();
		}
	}

Optional Assertions

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

	@Test
	public void optionalAssertions() {
		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]

Predicate Assertions

Assertions Java 8 Features 驗證是否符合條件,若不成立,則會拋出 AssertionError 。

	@Test
	public void predicateAssertions() {
		Predicate<String> lengthGreaterThan = s -> s.length() > 3;
		System.out.println(lengthGreaterThan);
		assertThat(lengthGreaterThan).accepts("AssertJ", "JUnit");

		Predicate<Integer> greaterThan = i -> i > 10;
		System.out.println(greaterThan);
		assertThat(greaterThan).accepts(155, 151);
	}
org.ruoxue.spring_boot_168.test.assertj.AssertJJava8FeaturesTest$$Lambda$277/1975546571@1f1c7bf6
org.ruoxue.spring_boot_168.test.assertj.AssertJJava8FeaturesTest$$Lambda$282/2021707251@5be6e01c

LocalDateTime Assertions

Assertions Java 8 Features 驗證日期時間是否在指定日期時間之前,若不成立,則會拋出 AssertionError 。

	@Test
	public void localDateAssertions() {
		LocalDateTime value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
		System.out.println(value);
		assertThat(value).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
		assertThat(value).isBefore(LocalDateTime.of(2023, 10, 31, 5, 7, 7));
	}
2023-10-31T05:06:07

FlatExtracting

Assertions Java 8 Features 驗證取得根據類名或方法引用或屬性名反射調用,或根據 lambda 表達式調用,提取元素屬性符合條件,若不成立,則會拋出 AssertionError 。

	@Test
	public void flatExtracting() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
		Fruit banana = new Fruit("Banana", 1, 2, Arrays.asList("Canada", "Norway"));
		Fruit cherry = new Fruit("Cherry", -1, 3, Arrays.asList("Poland", "Japan"));
		List<Fruit> list = Arrays.asList(apple, banana, cherry);
		System.out.println(list);
		assertThat(list).flatExtracting("origins").containsOnly("Australia", "Canada", "Norway", "Poland", "Japan");
	}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1,"origins":[Australia]}, {"name":"Banana","quantity":1.0,"type":2,"origins":[Canada, Norway]}, {"name":"Cherry","quantity":-1.0,"type":3,"origins":[Poland, Japan]}]

Satisfies

Asserting Java 8 Features with Examples 驗證是否符合條件,若不成立,則會拋出 AssertionError 。

	@Test
	public void satisfies() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).satisfies(s -> {
			assertThat(s).isNotEmpty();
			assertThat(s).hasSize(11);
		});

		int intValue = 155;
		System.out.println(intValue);
		assertThat(intValue).satisfies(i -> {
			assertThat(i).isNotZero();
			assertThat(i).isLessThan(156);
		});
	}
AssertJ 155
155

Matches

Asserting Java 8 Features with Examples 驗證是否符合條件,若不成立,則會拋出 AssertionError 。

	@Test
	public void matches() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).matches(s -> s.length() < 12);

		int intValue = 155;
		System.out.println(intValue);
		assertThat(intValue).matches(i -> i < 156);
	}
AssertJ 155
155

AssertJJava8FeaturesTest.java

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

package org.ruoxue.spring_boot_168.test.assertj;

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

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

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

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

public class AssertJJava8FeaturesTest {

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	public static class Fruit {
		private String name;
		private double quantity;
		private int type;
		private List<String> origins = new ArrayList<>();

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

		public Fruit(String name, double quantity, int type) {
			this(name, quantity, type, new ArrayList<>());
		}

		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);
			builder.append("origins", origins);
			return builder.toString();
		}
	}

	@Test
	public void optionalAssertions() {
		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 predicateAssertions() {
		Predicate<String> lengthGreaterThan = s -> s.length() > 3;
		System.out.println(lengthGreaterThan);
		assertThat(lengthGreaterThan).accepts("AssertJ", "JUnit");

		Predicate<Integer> greaterThan = i -> i > 10;
		System.out.println(greaterThan);
		assertThat(greaterThan).accepts(155, 151);
	}

	@Test
	public void localDateAssertions() {
		LocalDateTime value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
		System.out.println(value);
		assertThat(value).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
		assertThat(value).isBefore(LocalDateTime.of(2023, 10, 31, 5, 7, 7));
	}

	@Test
	public void flatExtracting() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1, Arrays.asList("Australia"));
		Fruit banana = new Fruit("Banana", 1, 2, Arrays.asList("Canada", "Norway"));
		Fruit cherry = new Fruit("Cherry", -1, 3, Arrays.asList("Poland", "Japan"));
		List<Fruit> list = Arrays.asList(apple, banana, cherry);
		System.out.println(list);
		assertThat(list).flatExtracting("origins").containsOnly("Australia", "Canada", "Norway", "Poland", "Japan");
	}

	@Test
	public void satisfies() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).satisfies(s -> {
			assertThat(s).isNotEmpty();
			assertThat(s).hasSize(11);
		});

		int intValue = 155;
		System.out.println(intValue);
		assertThat(intValue).satisfies(i -> {
			assertThat(i).isNotZero();
			assertThat(i).isLessThan(156);
		});
	}

	@Test
	public void matches() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).matches(s -> s.length() < 12);

		int intValue = 155;
		System.out.println(intValue);
		assertThat(intValue).matches(i -> i < 156);
	}
}

心得分享

Testing Java 8 with AssertJ 提供 Java 8 的新特色,像是 Optional 、 Predicate 、 LocalDateTime 等物件及 flatExtracting 、 satisfies 、 matches 輔助方法,進行斷言,驗證是否成立,在許多測試驗證的場景,讓開發者使用更流暢的單元測試,善用 Asserting Java 8 Features with Examples 將有助於驗證效率的提升。

發佈留言