Table of Contents
ToggleTesting with AssertJ Predicate in Java
提供驗證 Predicate 條件判斷等方法,編寫的測試程式使用流暢的斷言語句,通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,使用 Testing Java AssertJ Predicate 流式斷言,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- predicate
| +- TestingAssertJPredicateTest.java
單元測試
Java AssertJ Predicate Testing 斷言 Predicate 的主要目的是取得 Predicate 以進行斷言。
rejectsAll
驗證是否拒絕條件,若不成立,則會拋出 AssertionError 。
@Test
public void rejectsAll() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).rejectsAll(newArrayList("155", "151"));
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).rejectsAll(newArrayList(1, 2));
}
org.ruoxue.spring_boot_168.test.assertj.predicate.TestingAssertJPredicateTest$$Lambda$277/1975546571@1f1c7bf6
org.ruoxue.spring_boot_168.test.assertj.predicate.TestingAssertJPredicateTest$$Lambda$282/1861781750@10e92f8f
rejectsAllWithNegate
Java AssertJ Predicate Testing 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void rejectsAllWithNegate() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan.negate());
assertThat(lengthGreaterThan.negate()).rejectsAll(newArrayList("AssertJ", "JUnit"));
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan.negate());
assertThat(greaterThan.negate()).rejectsAll(newArrayList(155, 151));
}
java.util.function.Predicate$$Lambda$278/521960438@25b485ba
java.util.function.Predicate$$Lambda$278/521960438@7ce3cb8e
rejectsAllWithAnd
Java Testing AssertJ Predicate 驗證是否拒絕 and 條件,若不成立,則會拋出 AssertionError 。
@Test
public void rejectsAllWithAnd() {
Predicate<String> startsWith = s -> s.startsWith("J");
Predicate<String> endsWith = s -> s.endsWith("A");
Predicate<String> predicate = startsWith.and(endsWith);
System.out.println(predicate);
assertThat(predicate).rejectsAll(newArrayList("AssertJ", "AJ"));
Predicate<Integer> greaterThan = i -> i > 3;
Predicate<Integer> lessThan = i -> i < 6;
Predicate<Integer> intPredicate = greaterThan.and(lessThan);
System.out.println(intPredicate);
assertThat(intPredicate).rejectsAll(newArrayList(2, 7));
}
java.util.function.Predicate$$Lambda$214/599491651@25b485ba
java.util.function.Predicate$$Lambda$214/599491651@78b66d36
rejectsAllWithOr
Java Testing AssertJ Predicate 驗證是否拒絕 or 條件,若不成立,則會拋出 AssertionError 。
@Test
public void rejectsAllWithOr() {
Predicate<String> startsWith = s -> s.startsWith("t");
Predicate<String> endsWith = s -> s.endsWith("A");
Predicate<String> predicate = startsWith.or(endsWith);
System.out.println(predicate);
assertThat(predicate).rejectsAll(newArrayList("AssertJ", "JUnit"));
Predicate<Integer> greaterThan = i -> i < 3;
Predicate<Integer> lessThan = i -> i > 6;
Predicate<Integer> intPredicate = greaterThan.or(lessThan);
System.out.println(intPredicate);
assertThat(intPredicate).rejectsAll(newArrayList(4, 5));
}
java.util.function.Predicate$$Lambda$38/2034688500@25b485ba
java.util.function.Predicate$$Lambda$38/2034688500@78b66d36
rejectsAllWithChaining
Java Testing AssertJ Predicate 驗證是否拒絕鏈結條件,若不成立,則會拋出 AssertionError 。
@Test
public void rejectsAllWithChaining() {
Predicate<String> isNull = Objects::isNull;
Predicate<String> startsWith = s -> s.startsWith("t");
Predicate<String> contains = Pattern.compile("\\wX").asPredicate();
Predicate<String> chaining = isNull.and(startsWith).or(contains);
System.out.println(chaining);
assertThat(chaining).rejectsAll(newArrayList("AssertJ", "JUnit"));
Predicate<Integer> intIsNull = Objects::isNull;
Predicate<Integer> greaterThan = i -> i < 3;
Predicate<Integer> lessThan = i -> i > 6;
Predicate<Integer> intChaining = intIsNull.and(greaterThan).or(lessThan);
System.out.println(intChaining);
assertThat(intChaining).rejectsAll(newArrayList(4, 5));
}
java.util.function.Predicate$$Lambda$38/2034688500@5d740a0f
java.util.function.Predicate$$Lambda$38/2034688500@69b2283a
TestingAssertJPredicateTest.java
Java Testing AssertJ Predicate 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.predicate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
public class TestingAssertJPredicateTest {
@Test
public void rejectsAll() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan);
assertThat(lengthGreaterThan).rejectsAll(newArrayList("155", "151"));
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan);
assertThat(greaterThan).rejectsAll(newArrayList(1, 2));
}
@Test
public void rejectsAllWithNegate() {
Predicate<String> lengthGreaterThan = s -> s.length() > 3;
System.out.println(lengthGreaterThan.negate());
assertThat(lengthGreaterThan.negate()).rejectsAll(newArrayList("AssertJ", "JUnit"));
Predicate<Integer> greaterThan = i -> i > 10;
System.out.println(greaterThan.negate());
assertThat(greaterThan.negate()).rejectsAll(newArrayList(155, 151));
}
@Test
public void rejectsAllWithAnd() {
Predicate<String> startsWith = s -> s.startsWith("J");
Predicate<String> endsWith = s -> s.endsWith("A");
Predicate<String> predicate = startsWith.and(endsWith);
System.out.println(predicate);
assertThat(predicate).rejectsAll(newArrayList("AssertJ", "AJ"));
Predicate<Integer> greaterThan = i -> i > 3;
Predicate<Integer> lessThan = i -> i < 6;
Predicate<Integer> intPredicate = greaterThan.and(lessThan);
System.out.println(intPredicate);
assertThat(intPredicate).rejectsAll(newArrayList(2, 7));
}
@Test
public void rejectsAllWithOr() {
Predicate<String> startsWith = s -> s.startsWith("t");
Predicate<String> endsWith = s -> s.endsWith("A");
Predicate<String> predicate = startsWith.or(endsWith);
System.out.println(predicate);
assertThat(predicate).rejectsAll(newArrayList("AssertJ", "JUnit"));
Predicate<Integer> greaterThan = i -> i < 3;
Predicate<Integer> lessThan = i -> i > 6;
Predicate<Integer> intPredicate = greaterThan.or(lessThan);
System.out.println(intPredicate);
assertThat(intPredicate).rejectsAll(newArrayList(4, 5));
}
@Test
public void rejectsAllWithChaining() {
Predicate<String> isNull = Objects::isNull;
Predicate<String> startsWith = s -> s.startsWith("t");
Predicate<String> contains = Pattern.compile("\\wX").asPredicate();
Predicate<String> chaining = isNull.and(startsWith).or(contains);
System.out.println(chaining);
assertThat(chaining).rejectsAll(newArrayList("AssertJ", "JUnit"));
Predicate<Integer> intIsNull = Objects::isNull;
Predicate<Integer> greaterThan = i -> i < 3;
Predicate<Integer> lessThan = i -> i > 6;
Predicate<Integer> intChaining = intIsNull.and(greaterThan).or(lessThan);
System.out.println(intChaining);
assertThat(intChaining).rejectsAll(newArrayList(4, 5));
}
}
心得分享
AssertJ Predicate Java Testing 除了提供流式判斷,還針對 Predicate 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈,善用 Java Testing AssertJ Predicate 將有助於驗證效率的提升。