Table of Contents
ToggleAssertJ Object in Java with Examples
為用 Java 編寫的測試程式提供了流暢的斷言語句,這些斷言語句通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,驗證 Object 是否相同元素、大小, AssertJ Object in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- object
| +- AssertJObjectWithExamplesTest.java
單元測試
AssertJ Object Java 斷言物件的主要目的是取得物件以進行斷言。
has
驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void has() {
Object value = "AssertJ";
System.out.println(value);
Condition<Object> equals = new Condition<Object>(o -> o.equals("AssertJ"), "equals");
assertThat(value).has(equals);
value = BigDecimal.valueOf(155);
System.out.println(value);
Condition<Object> toString = new Condition<Object>(o -> o.toString().equals("155"), "toString");
assertThat(value).has(toString);
}
AssertJ
155
hasSameClassAs
AssertJ Object Java 驗證類別是否相同,若不成立,則會拋出 AssertionError 。
@Test
public void hasSameClassAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).hasSameClassAs("151");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).hasSameClassAs(BigDecimal.valueOf(151));
}
AssertJ
155
hasSameHashCodeAs
AssertJ Object Java 驗證 hash code 是否相同,若不成立,則會拋出 AssertionError 。
@Test
public void hasSameHashCodeAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).hasSameHashCodeAs("AssertJ");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).hasSameHashCodeAs(BigDecimal.valueOf(155));
}
AssertJ
155
hasToString
AssertJ Object Java 驗證 toString 是否相同,若不成立,則會拋出 AssertionError 。
@Test
public void hasToString() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).hasToString("AssertJ");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).hasToString("155");
}
AssertJ
155
doesNotHave
AssertJ Object Java 驗證是否不符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHave() {
Object value = "AssertJ";
System.out.println(value);
Condition<Object> equals = new Condition<Object>(o -> o.equals("155"), "equals");
assertThat(value).doesNotHave(equals);
value = BigDecimal.valueOf(155);
System.out.println(value);
Condition<Object> toString = new Condition<Object>(o -> o.toString().equals("AssertJ"), "toString");
assertThat(value).doesNotHave(toString);
}
AssertJ
155
doesNotHaveSameClassAs
Java AssertJ Object 驗證類別是否不相同,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHaveSameClassAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotHaveSameClassAs(BigDecimal.valueOf(155));
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).doesNotHaveSameClassAs("AssertJ");
}
AssertJ
155
doesNotHaveSameHashCodeAs
Java AssertJ Object 驗證 hash code 是否不相同,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHaveSameHashCodeAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotHaveSameHashCodeAs(BigDecimal.valueOf(155));
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).doesNotHaveSameHashCodeAs("AssertJ");
}
AssertJ
155
doesNotHaveToString
Java AssertJ Object 驗證 toString 是否不相同,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHaveToString() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotHaveToString("155");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).doesNotHaveToString("AssertJ");
}
AssertJ
155
AssertJObjectWithExamplesTest.java
Java AssertJ Object 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.object;
import static org.assertj.core.api.Assertions.*;
import java.math.BigDecimal;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
public class AssertJObjectWithExamplesTest {
@Test
public void has() {
Object value = "AssertJ";
System.out.println(value);
Condition<Object> equals = new Condition<Object>(o -> o.equals("AssertJ"), "equals");
assertThat(value).has(equals);
value = BigDecimal.valueOf(155);
System.out.println(value);
Condition<Object> toString = new Condition<Object>(o -> o.toString().equals("155"), "toString");
assertThat(value).has(toString);
}
@Test
public void hasSameClassAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).hasSameClassAs("151");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).hasSameClassAs(BigDecimal.valueOf(151));
}
@Test
public void hasSameHashCodeAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).hasSameHashCodeAs("AssertJ");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).hasSameHashCodeAs(BigDecimal.valueOf(155));
}
@Test
public void hasToString() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).hasToString("AssertJ");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).hasToString("155");
}
@Test
public void doesNotHave() {
Object value = "AssertJ";
System.out.println(value);
Condition<Object> equals = new Condition<Object>(o -> o.equals("155"), "equals");
assertThat(value).doesNotHave(equals);
value = BigDecimal.valueOf(155);
System.out.println(value);
Condition<Object> toString = new Condition<Object>(o -> o.toString().equals("AssertJ"), "toString");
assertThat(value).doesNotHave(toString);
}
@Test
public void doesNotHaveSameClassAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotHaveSameClassAs(BigDecimal.valueOf(155));
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).doesNotHaveSameClassAs("AssertJ");
}
@Test
public void doesNotHaveSameHashCodeAs() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotHaveSameHashCodeAs(BigDecimal.valueOf(155));
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).doesNotHaveSameHashCodeAs("AssertJ");
}
@Test
public void doesNotHaveToString() {
Object value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotHaveToString("155");
value = BigDecimal.valueOf(155);
System.out.println(value);
assertThat(value).doesNotHaveToString("AssertJ");
}
}
心得分享
Java AssertJ Object Example 提高了測試程式碼的可讀性,基本方法是 assertThat 方法,善用 Java AssertJ Object 將有助於驗證效率的提升。