Table of Contents
ToggleUnit Test AssertJ Object with Examples
介紹 containsAnyOf 、 containsAtIndex 、 containsSequence 、 containsNull 等方法,使用流式斷言,流暢地寫完驗證,不須再寫迴圈, AssertJ Object Unit Test with Examples 在許多測試驗證場景,提供開發者更流暢的驗證 API,驗證更直覺,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- object
| +- UnitTestAssertJObjectWithExamplesTest.java
單元測試
Examples AssertJ Object Unit Test 斷言物件的主要目的是取得物件以進行斷言。
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();
}
}
containsAnyOf
驗證包含任一字串,若不成立,則會拋出 AssertionError 。
@Test
public void containsAnyOf() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsAnyOf("Litchi", "Pomelo");
assertThat(value).extracting("name").asInstanceOf(STRING).containsAnyOf("101", "99");
}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
containsIgnoringCase
Examples AssertJ Object Unit Test 驗證不分大小寫包含物件,若不成立,則會拋出 AssertionError 。
@Test
public void containsIgnoringCase() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringCase("litchi");
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringCase("CHI");
}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
doesNotContainIgnoringCase
Examples AssertJ Object Unit Test 驗證不分大小寫不包含物件,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainIgnoringCase() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainIgnoringCase("pomelo");
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainIgnoringCase("ELO");
}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
containsIgnoringWhitespaces
Examples AssertJ Object Unit Test 驗證不分空白包含物件,若不成立,則會拋出 AssertionError 。
@Test
public void containsIgnoringWhitespaces() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringWhitespaces("Litchi ");
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringWhitespaces(" 101");
}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
containsIgnoringNewLines
Examples AssertJ Object Unit Test 驗證不分斷列包含物件,若不成立,則會拋出 AssertionError 。
@Test
public void containsIgnoringNewLines() {
Object value = new Fruit("Litc\nhi 1\n01\n", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringNewLines("Litchi", "101");
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringNewLines("chi", "101");
}
{"name":"Litc\nhi 1\n01\n","quantity":1.7976931348623157E308,"type":1}
containsPattern
Examples Unit Test AssertJ Object 驗證符合正則條件的物件,若不成立,則會拋出 AssertionError 。
@Test
public void containsPattern() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsPattern("Li.c");
assertThat(value).extracting("name").asInstanceOf(STRING).containsPattern("1\\d1");
}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
doesNotContainPattern
Examples Unit Test AssertJ Object 驗證不符合正則條件的物件,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainPattern() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainPattern("Li.x");
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainPattern("1\\d9");
}
{"name":"Litchi 101","quantity":1.7976931348623157E308,"type":1}
containsSequence
Examples Unit Test AssertJ Object 驗證依序包含物件,若不成立,則會拋出 AssertionError 。
@Test
public void containsSequence() {
Object value = new Fruit("Litchi 101 Pomelo 99", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsSequence("Litchi", " ", "101");
assertThat(value).extracting("name").asInstanceOf(STRING).containsSequence(Arrays.asList("Litchi", " ", "101"));
assertThat(value).extracting("name").asInstanceOf(STRING).containsSequence("Pomelo", " ", "99");
}
{"name":"Litchi 101 Pomelo 99","quantity":1.7976931348623157E308,"type":1}
containsSubsequence
Examples Unit Test AssertJ Object 驗證依序包含物件,若不成立,則會拋出 AssertionError 。
@Test
public void containsSubsequence() {
Object value = new Fruit("Litchi 101 Pomelo 99", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsSubsequence("Litchi", "Pomelo");
assertThat(value).extracting("name").asInstanceOf(STRING)
.containsSubsequence(Arrays.asList("Litchi", "Pomelo"));
assertThat(value).extracting("name").asInstanceOf(STRING).containsSubsequence("101", " ", "99");
}
{"name":"Litchi 101 Pomelo 99","quantity":1.7976931348623157E308,"type":1}
UnitTestAssertJObjectWithExamplesTest.java
Examples Unit Test AssertJ Object 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.object;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.STRING;
import java.util.Arrays;
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 UnitTestAssertJObjectWithExamplesTest {
@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 containsAnyOf() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsAnyOf("Litchi", "Pomelo");
assertThat(value).extracting("name").asInstanceOf(STRING).containsAnyOf("101", "99");
}
@Test
public void containsIgnoringCase() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringCase("litchi");
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringCase("CHI");
}
@Test
public void doesNotContainIgnoringCase() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainIgnoringCase("pomelo");
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainIgnoringCase("ELO");
}
@Test
public void containsIgnoringWhitespaces() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringWhitespaces("Litchi ");
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringWhitespaces(" 101");
}
@Test
public void containsIgnoringNewLines() {
Object value = new Fruit("Litc\nhi 1\n01\n", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringNewLines("Litchi", "101");
assertThat(value).extracting("name").asInstanceOf(STRING).containsIgnoringNewLines("chi", "101");
}
@Test
public void containsPattern() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsPattern("Li.c");
assertThat(value).extracting("name").asInstanceOf(STRING).containsPattern("1\\d1");
}
@Test
public void doesNotContainPattern() {
Object value = new Fruit("Litchi 101", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainPattern("Li.x");
assertThat(value).extracting("name").asInstanceOf(STRING).doesNotContainPattern("1\\d9");
}
@Test
public void containsSequence() {
Object value = new Fruit("Litchi 101 Pomelo 99", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsSequence("Litchi", " ", "101");
assertThat(value).extracting("name").asInstanceOf(STRING).containsSequence(Arrays.asList("Litchi", " ", "101"));
assertThat(value).extracting("name").asInstanceOf(STRING).containsSequence("Pomelo", " ", "99");
}
@Test
public void containsSubsequence() {
Object value = new Fruit("Litchi 101 Pomelo 99", Double.MAX_VALUE, 1);
System.out.println(value);
assertThat(value).extracting("name").asInstanceOf(STRING).containsSubsequence("Litchi", "Pomelo");
assertThat(value).extracting("name").asInstanceOf(STRING)
.containsSubsequence(Arrays.asList("Litchi", "Pomelo"));
assertThat(value).extracting("name").asInstanceOf(STRING).containsSubsequence("101", " ", "99");
}
}
心得分享
Unit Test AssertJ Object Example 斷言的基本方法是 assertThat 方法,提高了測試程式碼的可讀性,並且被設計為在主流 IDE 中易於使用,讓開發者將繁瑣的測試驗證過程,變為更簡單,善用 Examples Unit Test AssertJ Object 將有助於驗證效率的提升。