Table of Contents
ToggleAsserting Optionals with AssertJ
介紹 matches 、 satisfies 驗證是否符合條件,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Assert Optionals in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- optional
| +- AssertingOptionalsTest.java
單元測試
Assertions Optionals in Java 斷言 Optional 的主要目的是取得 Optional 以進行斷言。
matches
Assertions Optionals in Java 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void matches() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).matches(o -> o.isPresent());
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).matches(o -> o.get() != null);
}
Optional[AssertJ]
Optional[155]
matchesWithDescription
Assertions Optionals in Java 驗證是否符合條件,自訂描述訊息,若不成立,則會拋出 AssertionError 。
@Test
public void matchesWithDescription() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).matches(o -> o.isPresent(), "isPresent");
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).matches(o -> o.get() != null, "notNull");
}
Optional[AssertJ]
Optional[155]
matchesThrowError
Assertions Optionals in Java 驗證是否符合條件,自訂描述訊息,當不成立時,驗證拋出 AssertionError 。
@Test
public void matchesThrowError() {
assertThatCode(() -> {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).matches(o -> false == o.isPresent(), "isPresent");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).matches(o -> o.get() == null, "notNull");
}).isInstanceOf(AssertionError.class);
}
Optional[AssertJ]
Optional[155]
satisfies
Asserting Optionals with Examples 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfies() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).satisfies(o -> {
assertThat(o).isNotEmpty();
assertThat(o).hasValue("AssertJ");
});
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).satisfies(o -> {
assertThat(o).isNotNull();
}, o -> {
assertThat(o).isIn(Optional.of(151), Optional.of(155));
});
}
Optional[AssertJ]
Optional[155]
satisfiesAnyOf
Asserting Optionals with Examples 驗證是否符合任一條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfiesAnyOf() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).satisfiesAnyOf(o -> {
assertThat(o).isNotEmpty();
assertThat(o).hasValue("AssertJ");
});
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).satisfiesAnyOf(o -> {
assertThat(o).isNotNull();
}, o -> {
assertThat(o).isIn(Optional.of(0), Optional.of(1));
});
}
Optional[AssertJ]
Optional[155]
satisfiesThrowError
Asserting Optionals with Examples 驗證是否符合條件,當不成立時,驗證拋出 AssertionError 。
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).satisfies(o -> {
assertThat(o).isNotEmpty();
assertThat(o).hasValue("155");
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).satisfies(o -> {
assertThat(o).isNotNull();
}, o -> {
assertThat(o).isIn(Optional.of(151), Optional.of(165));
});
}).isInstanceOf(AssertionError.class);
}
Optional[AssertJ]
Optional[155]
AssertingOptionalsTest.java
Asserting Optionals with Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import java.util.Optional;
import org.junit.jupiter.api.Test;
public class AssertingOptionalsTest {
@Test
public void matches() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).matches(o -> o.isPresent());
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).matches(o -> o.get() != null);
}
@Test
public void matchesWithDescription() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).matches(o -> o.isPresent(), "isPresent");
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).matches(o -> o.get() != null, "notNull");
}
@Test
public void matchesThrowError() {
assertThatCode(() -> {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).matches(o -> false == o.isPresent(), "isPresent");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).matches(o -> o.get() == null, "notNull");
}).isInstanceOf(AssertionError.class);
}
@Test
public void satisfies() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).satisfies(o -> {
assertThat(o).isNotEmpty();
assertThat(o).hasValue("AssertJ");
});
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).satisfies(o -> {
assertThat(o).isNotNull();
}, o -> {
assertThat(o).isIn(Optional.of(151), Optional.of(155));
});
}
@Test
public void satisfiesAnyOf() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).satisfiesAnyOf(o -> {
assertThat(o).isNotEmpty();
assertThat(o).hasValue("AssertJ");
});
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).satisfiesAnyOf(o -> {
assertThat(o).isNotNull();
}, o -> {
assertThat(o).isIn(Optional.of(0), Optional.of(1));
});
}
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).satisfies(o -> {
assertThat(o).isNotEmpty();
assertThat(o).hasValue("155");
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).satisfies(o -> {
assertThat(o).isNotNull();
}, o -> {
assertThat(o).isIn(Optional.of(151), Optional.of(165));
});
}).isInstanceOf(AssertionError.class);
}
}
心得分享
Testing Java Optionals with AssertJ 快速檢查物件是否滿足所有提供的斷言,搭配使用 Predicate 與 ThrowingConsumer 等接口,根據 lambda 表達式調用,驗證是否成立,若不成立,則會拋出 AssertionError, Asserting Optionals with Examples 介紹 matches 、 satisfies 等方法,提供範例參考。
原碼下載