Table of Contents
ToggleAsserting Classes with AssertJ
介紹 matches 、 satisfies 驗證是否符合條件,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Assert Classes in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- clazz
| +- AssertingClassesTest.java
單元測試
Assertions Classes in Java 斷言類別的主要目的是取得類別以進行斷言。
Fruit
Assertions Classes 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();
}
}
matches
Assertions Classes in Java 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void matches() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isInterface());
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).matches(c -> c.isPrimitive());
}value = "AssertJ";
System.out.println(value);
assertThat(value).matches(s -> s.length() < 8);
}
interface java.lang.CharSequence
int
matchesWithDescription
Assertions Classes in Java 驗證是否符合條件,自訂描述訊息,若不成立,則會拋出 AssertionError 。
@Test
public void matchesWithDescription() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isInterface(), "interface");
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).matches(c -> c.isPrimitive(), "primitive");
}
interface java.lang.CharSequence
int
matchesThrowError
Assertions Classes in Java 驗證是否符合條件,自訂描述訊息,當不成立時,驗證拋出 AssertionError 。
@Test
public void matchesThrowError() {
assertThatCode(() -> {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isPrimitive(), "primitive");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Class<?> clazz = int.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isInterface(), "interface");
}).isInstanceOf(AssertionError.class);
}
interface java.lang.CharSequence
int
satisfies
Asserting Classes with Examples 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfies() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isInterface();
assertThat(c).isPublic();
});
clazz = int.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isAbstract();
}, c -> {
assertThat(c).isFinal();
});
}
interface java.lang.CharSequence
int
satisfiesAnyOf
Asserting Classes with Examples 驗證是否符合任一條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfiesAnyOf() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).satisfiesAnyOf(c -> {
assertThat(c).isInterface();
assertThat(c).isPublic();
});
clazz = int.class;
System.out.println(clazz);
assertThat(clazz).satisfiesAnyOf(c -> {
assertThat(c).isAbstract();
}, c -> {
assertThat(c).isAnnotation();
});
}
interface java.lang.CharSequence
int
satisfiesThrowError
Asserting Classes with Examples 驗證是否符合條件,當不成立時,驗證拋出 AssertionError 。
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isInterface();
assertThat(c).isProtected();
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Class<?> clazz = int.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isAbstract();
}, c -> {
assertThat(c).isAnnotation();
});
}).isInstanceOf(AssertionError.class);
}
interface java.lang.CharSequence
int
AssertingClassesTest.java
Asserting Classes with Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.clazz;
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.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class AssertingClassesTest {
@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 matches() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isInterface());
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).matches(c -> c.isPrimitive());
}
@Test
public void matchesWithDescription() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isInterface(), "interface");
Class<?> clazz2 = int.class;
System.out.println(clazz2);
assertThat(clazz2).matches(c -> c.isPrimitive(), "primitive");
}
@Test
public void matchesThrowError() {
assertThatCode(() -> {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isPrimitive(), "primitive");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Class<?> clazz = int.class;
System.out.println(clazz);
assertThat(clazz).matches(c -> c.isInterface(), "interface");
}).isInstanceOf(AssertionError.class);
}
@Test
public void satisfies() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isInterface();
assertThat(c).isPublic();
});
clazz = int.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isAbstract();
}, c -> {
assertThat(c).isFinal();
});
}
@Test
public void satisfiesAnyOf() {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).satisfiesAnyOf(c -> {
assertThat(c).isInterface();
assertThat(c).isPublic();
});
clazz = int.class;
System.out.println(clazz);
assertThat(clazz).satisfiesAnyOf(c -> {
assertThat(c).isAbstract();
}, c -> {
assertThat(c).isAnnotation();
});
}
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
Class<?> clazz = CharSequence.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isInterface();
assertThat(c).isProtected();
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Class<?> clazz = int.class;
System.out.println(clazz);
assertThat(clazz).satisfies(c -> {
assertThat(c).isAbstract();
}, c -> {
assertThat(c).isAnnotation();
});
}).isInstanceOf(AssertionError.class);
}
}
心得分享
Testing Java Classes with AssertJ 快速檢查物件是否滿足所有提供的斷言,搭配使用 Predicate 與 ThrowingConsumer 等接口,根據 lambda 表達式調用,驗證是否成立,若不成立,則會拋出 AssertionError, Asserting Classes with Examples 介紹 matches 、 satisfies 等方法,提供範例參考。