Table of Contents
ToggleAssertJ String Assertions
提供驗證包含、不包含指定的字串等方法, AssertJ 是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Asserting String with AssertJ 使用流式斷言,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- string
| +- AssertJStringAssertionsTest.java
單元測試
String Elements with AssertJ 斷言字串的主要目的是取得字串以進行斷言。
contains
驗證包含指定的字串,若不成立,則會拋出 AssertionError 。
@Test
public void contains() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).contains("AssertJ", "155");
assertThat(value).contains("rtJ", "5");
}
AssertJ 155
doesNotContain
String Elements with AssertJ 驗證 不包含指定的字串,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContain() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).doesNotContain("JUnit", "151");
assertThat(value).doesNotContain("nit", "51");
}
AssertJ 155
containsOnlyOnce
String Elements with AssertJ 驗證包含只出現一次的字串,若不成立,則會拋出 AssertionError 。
@Test
public void containsOnlyOnce() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).containsOnlyOnce("rtJ");
assertThat(value).containsOnlyOnce("55");
}
AssertJ 155
containsOnlyWhitespaces
String Elements with AssertJ 驗證只包含空白,若不成立,則會拋出 AssertionError 。
@Test
public void containsOnlyWhitespaces() {
String value = " ";
System.out.println(value);
assertThat(value).containsOnlyWhitespaces();
value = " ";
System.out.println(value);
assertThat(value).containsOnlyWhitespaces();
}
doesNotContainOnlyWhitespaces
Testing Java String with AssertJ 驗證不只包含空白,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainOnlyWhitespaces() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).doesNotContainOnlyWhitespaces();
value = "AssertJ ";
System.out.println(value);
assertThat(value).doesNotContainOnlyWhitespaces();
}
AssertJ 155
AssertJ
containsOnlyDigits
Testing Java String with AssertJ 驗證只包含數字,若不成立,則會拋出 AssertionError 。
@Test
public void containsOnlyDigits() {
String value = "155";
System.out.println(value);
assertThat(value).containsOnlyDigits();
value = "5";
System.out.println(value);
assertThat(value).containsOnlyDigits();
}
155
5
containsWhitespaces
Testing Java String with AssertJ 驗證包含空白,若不成立,則會拋出 AssertionError 。
@Test
public void containsWhitespaces() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).containsWhitespaces();
value = " ";
System.out.println(value);
assertThat(value).containsWhitespaces();
}
AssertJ 155
doesNotContainAnyWhitespaces
Testing Java String with AssertJ 驗證不包含任何空白,若不成立,則會拋出 AssertionError 。
@Test
public void doesNotContainAnyWhitespaces() {
String value = "AssertJ155";
System.out.println(value);
assertThat(value).doesNotContainAnyWhitespaces();
value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotContainAnyWhitespaces();
}
AssertJ155
AssertJ
AssertJStringAssertionsTest.java
Testing Java String with AssertJ 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.string;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class AssertJStringAssertionsTest {
@Test
public void contains() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).contains("AssertJ", "155");
assertThat(value).contains("rtJ", "5");
}
@Test
public void doesNotContain() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).doesNotContain("JUnit", "151");
assertThat(value).doesNotContain("nit", "51");
}
@Test
public void containsOnlyOnce() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).containsOnlyOnce("rtJ");
assertThat(value).containsOnlyOnce("55");
}
@Test
public void containsOnlyWhitespaces() {
String value = " ";
System.out.println(value);
assertThat(value).containsOnlyWhitespaces();
value = " ";
System.out.println(value);
assertThat(value).containsOnlyWhitespaces();
}
@Test
public void doesNotContainOnlyWhitespaces() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).doesNotContainOnlyWhitespaces();
value = "AssertJ ";
System.out.println(value);
assertThat(value).doesNotContainOnlyWhitespaces();
}
@Test
public void containsOnlyDigits() {
String value = "155";
System.out.println(value);
assertThat(value).containsOnlyDigits();
value = "5";
System.out.println(value);
assertThat(value).containsOnlyDigits();
}
@Test
public void containsWhitespaces() {
String value = "AssertJ 155";
System.out.println(value);
assertThat(value).containsWhitespaces();
value = " ";
System.out.println(value);
assertThat(value).containsWhitespaces();
}
@Test
public void doesNotContainAnyWhitespaces() {
String value = "AssertJ155";
System.out.println(value);
assertThat(value).doesNotContainAnyWhitespaces();
value = "AssertJ";
System.out.println(value);
assertThat(value).doesNotContainAnyWhitespaces();
}
}
心得分享
Assert String Java 除了提供流式判斷,還針對 String 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈,善用 Testing Java String with AssertJ 將有助於驗證效率的提升。