Table of Contents
ToggleAssertJ Optional in Java with Examples
為用 Java 編寫的測試程式提供了流暢的斷言語句,這些斷言語句通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,驗證 Optional 是否符合條件, AssertJ Optional in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- optional
| +- AssertJOptionalWithExamplesTest.java
單元測試
AssertJ Optional Java 斷言 Optional 的主要目的是取得 Optional 以進行斷言。
has
驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void has() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
Condition<Optional<String>> lengthGreaterThan = new Condition<Optional<String>>(o -> o.get().length() > 2, "lengthGreaterThan");
assertThat(value).has(lengthGreaterThan);
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
Condition<Optional<Integer>> greaterThan = new Condition<Optional<Integer>>(o -> o.get() > 0, "greaterThan");
assertThat(intValue).has(greaterThan);
}
Optional[AssertJ]
Optional[155]
hasToString
AssertJ Optional Java 驗證 toString 是否相等,若不成立,則會拋出 AssertionError 。
@Test
public void hasToString() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasToString(value.toString());
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasToString(intValue.toString());
}
Optional[AssertJ]
Optional[155]
hasSameClassAs
AssertJ Optional Java 驗證類別是否相等,若不成立,則會拋出 AssertionError 。
@Test
public void hasSameClassAs() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasSameClassAs(Optional.of("Unit Test"));
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasSameClassAs(Optional.of(151));
}
Optional[AssertJ]
Optional[155]
hasValue
AssertJ Optional Java 驗證是否有相同值,若不成立,則會拋出 AssertionError 。
@Test
public void hasValue() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasValue("AssertJ");
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasValue(155);
}
Optional[AssertJ]
Optional[155]
hasValueSatisfying
Java AssertJ Optional 驗證值是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void hasValueSatisfying() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasValueSatisfying(s -> {
assertThat(s).isEqualTo("AssertJ");
assertThat(s).startsWith("A");
assertThat(s).endsWith("J");
});
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasValueSatisfying(i -> {
assertThat(i).isEqualTo(155);
assertThat(i).isBetween(151, 155);
assertThat(i).isCloseTo(150, within(5));
});
}
Optional[AssertJ]
Optional[155]
hasValueSatisfyingWithCondition
Java AssertJ Optional 驗證值是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void hasValueSatisfyingWithCondition() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
Condition<String> lengthGreaterThan = new Condition<String>(s -> s.length() > 2, "lengthGreaterThan");
assertThat(value).hasValueSatisfying(lengthGreaterThan);
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
Condition<Integer> greaterThan = new Condition<Integer>(i -> i > 0, "greaterThan");
assertThat(intValue).hasValueSatisfying(greaterThan);
}
Optional[AssertJ]
Optional[155]
doesNotHave
Java AssertJ Optional Example 驗證是否不符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHave() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
Condition<Optional<String>> lengthLessThan = new Condition<Optional<String>>(o -> o.get().length() < 2,
"lengthLessThan");
assertThat(value).doesNotHave(lengthLessThan);
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
Condition<Optional<Integer>> greaterThan = new Condition<Optional<Integer>>(o -> o.get() > 155, "greaterThan");
assertThat(intValue).doesNotHave(greaterThan);
}
Optional[AssertJ]
Optional[155]
doesNotHaveSameClassAs
Java AssertJ Optional Example 驗證類別是否不相等,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHaveSameClassAs() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).doesNotHaveSameClassAs("Unit Test");
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).doesNotHaveSameClassAs(151);
}
Optional[AssertJ]
Optional[155]
AssertJOptionalWithExamplesTest.java
Java AssertJ Optional 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.optional;
import static org.assertj.core.api.Assertions.*;
import java.util.Optional;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
public class AssertJOptionalWithExamplesTest {
@Test
public void has() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
Condition<Optional<String>> lengthGreaterThan = new Condition<Optional<String>>(o -> o.get().length() > 2, "lengthGreaterThan");
assertThat(value).has(lengthGreaterThan);
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
Condition<Optional<Integer>> greaterThan = new Condition<Optional<Integer>>(o -> o.get() > 0, "greaterThan");
assertThat(intValue).has(greaterThan);
}
@Test
public void hasToString() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasToString(value.toString());
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasToString(intValue.toString());
}
@Test
public void hasSameClassAs() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasSameClassAs(Optional.of("Unit Test"));
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasSameClassAs(Optional.of(151));
}
@Test
public void hasValue() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasValue("AssertJ");
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasValue(155);
}
@Test
public void hasValueSatisfying() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).hasValueSatisfying(s -> {
assertThat(s).isEqualTo("AssertJ");
assertThat(s).startsWith("A");
assertThat(s).endsWith("J");
});
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).hasValueSatisfying(i -> {
assertThat(i).isEqualTo(155);
assertThat(i).isBetween(151, 155);
assertThat(i).isCloseTo(150, within(5));
});
}
@Test
public void hasValueSatisfyingWithCondition() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
Condition<String> lengthGreaterThan = new Condition<String>(s -> s.length() > 2, "lengthGreaterThan");
assertThat(value).hasValueSatisfying(lengthGreaterThan);
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
Condition<Integer> greaterThan = new Condition<Integer>(i -> i > 0, "greaterThan");
assertThat(intValue).hasValueSatisfying(greaterThan);
}
@Test
public void doesNotHave() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
Condition<Optional<String>> lengthLessThan = new Condition<Optional<String>>(o -> o.get().length() < 2,
"lengthLessThan");
assertThat(value).doesNotHave(lengthLessThan);
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
Condition<Optional<Integer>> greaterThan = new Condition<Optional<Integer>>(o -> o.get() > 155, "greaterThan");
assertThat(intValue).doesNotHave(greaterThan);
}
@Test
public void doesNotHaveSameClassAs() {
Optional<String> value = Optional.of("AssertJ");
System.out.println(value);
assertThat(value).doesNotHaveSameClassAs("Unit Test");
Optional<Integer> intValue = Optional.of(155);
System.out.println(intValue);
assertThat(intValue).doesNotHaveSameClassAs(151);
}
}
心得分享
Java AssertJ Optional Example 提高了測試程式碼的可讀性,基本方法是 assertThat 方法,善用 Java AssertJ Optional 將有助於驗證效率的提升。