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