Table of Contents
ToggleAssertJ String in Java with Examples
為用 Java 編寫的測試程式提供了流暢的斷言語句,這些斷言語句通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,驗證 String 是否相同元素、大小, AssertJ String in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- string
| +- AssertJStringWithExamplesTest.java
單元測試
AssertJ String Java 斷言字串的主要目的是取得字串以進行斷言。
has
驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void has() {
String value = "AssertJ 155";
System.out.println(value);
Condition<String> length = new Condition<String>(s -> s.length() > 2, "length");
assertThat(value).has(length);
value = null;
System.out.println(value);
Condition<String> nullz = new Condition<String>(s -> s == null, "null");
assertThat(value).has(nullz);
}
AssertJ 155
null
hasSize
AssertJ String Java 驗證大小是否為指定值,若不成立,則會拋出 AssertionError 。
@Test
public void hasSize() {
int expectedSize = 11;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSize(expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSize(7);
}
AssertJ 155
AssertJ
hasSizeBetween
AssertJ String Java 驗證大小是否介於起始值之間,若不成立,則會拋出 AssertionError 。
@Test
public void hasSizeBetween() {
int expectedSize = 11;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSizeBetween(1, expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSizeBetween(1, 7);
}
AssertJ 155
AssertJ
hasSizeGreaterThan
AssertJ String Java 驗證大小是否大於指定值,若不成立,則會拋出 AssertionError 。
@Test
public void hasSizeGreaterThan() {
int expectedSize = 2;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSizeGreaterThan(expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSizeGreaterThan(6);
}
AssertJ 155
AssertJ
hasSizeLessThan
Java AssertJ String 驗證大小是否小於指定值,若不成立,則會拋出 AssertionError 。
@Test
public void hasSizeLessThan() {
int expectedSize = 12;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSizeLessThan(expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSizeLessThan(8);
}
AssertJ 155
AssertJ
hasToString
Java AssertJ String 驗證 toString 是否相等,若不成立,則會拋出 AssertionError 。
@Test
public void hasToString() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasToString(value.toString());
value = "AssertJ";
System.out.println(value);
assertThat(value).hasToString(value.toString());
}
AssertJ 155
AssertJ
hasSameSizeAs
Java AssertJ String 驗證大小是否相等,若不成立,則會拋出 AssertionError 。
@Test
public void hasSameSizeAs() {
String value = "AssertJ 155";
String value2 = "AssertJ 155";
System.out.println(value);
System.out.println(value2);
assertThat(value).hasSameSizeAs(value2);
value = "AssertJ";
value2 = "AssertJ";
System.out.println(value);
System.out.println(value2);
assertThat(value).hasSameSizeAs(value2);
}
AssertJ 155
AssertJ 155
AssertJ
AssertJ
doesNotHave
Java AssertJ String Example 驗證是否不符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotHave() {
String value = "AssertJ 155";
System.out.println(value);
Condition<String> length = new Condition<String>(s -> s.length() > 12, "length");
assertThat(value).doesNotHave(length);
value = "AssertJ";
System.out.println(value);
Condition<String> nullz = new Condition<String>(s -> s == null, "null");
assertThat(value).doesNotHave(nullz);
}
AssertJ 155
AssertJ
AssertJStringWithExamplesTest.java
Java AssertJ String 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.string;
import static org.assertj.core.api.Assertions.*;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
public class AssertJStringWithExamplesTest {
@Test
public void has() {
String value = "AssertJ 155";
System.out.println(value);
Condition<String> length = new Condition<String>(s -> s.length() > 2, "length");
assertThat(value).has(length);
value = null;
System.out.println(value);
Condition<String> nullz = new Condition<String>(s -> s == null, "null");
assertThat(value).has(nullz);
}
@Test
public void hasSize() {
int expectedSize = 11;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSize(expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSize(7);
}
@Test
public void hasSizeBetween() {
int expectedSize = 11;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSizeBetween(1, expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSizeBetween(1, 7);
}
@Test
public void hasSizeGreaterThan() {
int expectedSize = 2;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSizeGreaterThan(expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSizeGreaterThan(6);
}
@Test
public void hasSizeLessThan() {
int expectedSize = 12;
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasSizeLessThan(expectedSize);
value = "AssertJ";
System.out.println(value);
assertThat(value).hasSizeLessThan(8);
}
@Test
public void hasToString() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).hasToString(value.toString());
value = "AssertJ";
System.out.println(value);
assertThat(value).hasToString(value.toString());
}
@Test
public void hasSameSizeAs() {
String value = "AssertJ 155";
String value2 = "AssertJ 155";
System.out.println(value);
System.out.println(value2);
assertThat(value).hasSameSizeAs(value2);
value = "AssertJ";
value2 = "AssertJ";
System.out.println(value);
System.out.println(value2);
assertThat(value).hasSameSizeAs(value2);
}
@Test
public void doesNotHave() {
String value = "AssertJ 155";
System.out.println(value);
Condition<String> length = new Condition<String>(s -> s.length() > 12, "length");
assertThat(value).doesNotHave(length);
value = "AssertJ";
System.out.println(value);
Condition<String> nullz = new Condition<String>(s -> s == null, "null");
assertThat(value).doesNotHave(nullz);
}
}
心得分享
Java AssertJ String Example 提高了測試程式碼的可讀性,基本方法是 assertThat 方法,善用 Java AssertJ String 將有助於驗證效率的提升。