Table of Contents
ToggleAsserting Predicates with AssertJ
介紹 matches 、 satisfies 驗證是否符合條件,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Assert Predicates in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- predicate
| +- AssertingPredicatesTest.java
單元測試
Assertions Predicates in Java 斷言 Predicate 的主要目的是取得 Predicate 以進行斷言。
matches
Assertions Predicates in Java 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void matches() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).matches(p -> p.test("AssertJ"));
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).matches(p -> p.test(155));
}
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$277/1975546571@1f1c7bf6
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$282/2021707251@5be6e01c
matchesWithDescription
Assertions Predicates in Java 驗證是否符合條件,自訂描述訊息,若不成立,則會拋出 AssertionError 。
@Test
public void matchesWithDescription() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).matches(p -> p.test("AssertJ"), "lengthGreaterThan");
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).matches(p -> p.test(155), "greaterThan");
}
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$277/1975546571@1f1c7bf6
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$282/2021707251@5be6e01c
matchesThrowError
Assertions Predicates in Java 驗證是否符合條件,自訂描述訊息,當不成立時,驗證拋出 AssertionError 。
@Test
public void matchesThrowError() {
assertThatCode(() -> {
Predicate<String> lengthGreaterThan = s -> s.length() < 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).matches(p -> p.test("AssertJ"), "lengthGreaterThan");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Predicate<Integer> greaterThan = i -> i < 10;
System.out.println(greaterThan);
assertThat(greaterThan).matches(p -> p.test(155), "greaterThan");
}).isInstanceOf(AssertionError.class);
}
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$278/2024453272@5dd6264
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$285/23211803@72a7c7e0
satisfies
Asserting Predicates with Examples 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfies() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).satisfies(p -> {
assertThat(p).accepts("AssertJ", "JUnit");
assertThat(p).rejects("155", "151");
});
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).satisfies(p -> {
assertThat(p).accepts(155, 151);
}, p -> {
assertThat(p).rejects(1, 2);
});
}
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$277/1975546571@1f1c7bf6
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$286/1704629915@76908cc0
satisfiesAnyOf
Asserting Predicates with Examples 驗證是否符合任一條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfiesAnyOf() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).satisfiesAnyOf(p -> {
assertThat(p).accepts("AssertJ", "JUnit");
assertThat(p).rejects("155", "151");
});
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).satisfiesAnyOf(p -> {
assertThat(p).accepts(155, 151);
}, p -> {
assertThat(p).rejects(100, 200);
});
}
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$277/1975546571@1f1c7bf6
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$286/1704629915@76908cc0
satisfiesThrowError
Asserting Predicates with Examples 驗證是否符合條件,當不成立時,驗證拋出 AssertionError 。
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).satisfies(p -> {
assertThat(p).accepts("AssertJ", "JUnit");
assertThat(p).rejects("9155", "9151");
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).satisfies(p -> {
assertThat(p).accepts(155, 151);
}, p -> {
assertThat(p).rejects(91, 92);
});
}).isInstanceOf(AssertionError.class);
}
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$278/2024453272@5dd6264
org.ruoxue.spring_boot_168.test.assertj.predicate.AssertingPredicatesTest$$Lambda$300/820914198@31d7b7bf
AssertingPredicatesTest.java
Asserting Predicates with Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.predicate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import java.util.function.Predicate;
import org.junit.jupiter.api.Test;
public class AssertingPredicatesTest {
@Test
public void matches() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).matches(p -> p.test("AssertJ"));
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).matches(p -> p.test(155));
}
@Test
public void matchesWithDescription() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).matches(p -> p.test("AssertJ"), "lengthGreaterThan");
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).matches(p -> p.test(155), "greaterThan");
}
@Test
public void matchesThrowError() {
assertThatCode(() -> {
Predicate<String> lengthGreaterThan = s -> s.length() < 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).matches(p -> p.test("AssertJ"), "lengthGreaterThan");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Predicate<Integer> greaterThan = i -> i < 10;
System.out.println(greaterThan);
assertThat(greaterThan).matches(p -> p.test(155), "greaterThan");
}).isInstanceOf(AssertionError.class);
}
@Test
public void satisfies() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).satisfies(p -> {
assertThat(p).accepts("AssertJ", "JUnit");
assertThat(p).rejects("155", "151");
});
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).satisfies(p -> {
assertThat(p).accepts(155, 151);
}, p -> {
assertThat(p).rejects(1, 2);
});
}
@Test
public void satisfiesAnyOf() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).satisfiesAnyOf(p -> {
assertThat(p).accepts("AssertJ", "JUnit");
assertThat(p).rejects("155", "151");
});
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).satisfiesAnyOf(p -> {
assertThat(p).accepts(155, 151);
}, p -> {
assertThat(p).rejects(100, 200);
});
}
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).satisfies(p -> {
assertThat(p).accepts("AssertJ", "JUnit");
assertThat(p).rejects("9155", "9151");
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).satisfies(p -> {
assertThat(p).accepts(155, 151);
}, p -> {
assertThat(p).rejects(91, 92);
});
}).isInstanceOf(AssertionError.class);
}
}
心得分享
Testing Java Predicates with AssertJ 快速檢查物件是否滿足所有提供的斷言,搭配使用 Predicate 與 ThrowingConsumer 等接口,根據 lambda 表達式調用,驗證是否成立,若不成立,則會拋出 AssertionError, Asserting Predicates with Examples 介紹 matches 、 satisfies 等方法,提供範例參考。
原碼下載