修改对象成使用的 String 字符串对象来进行说明

This commit is contained in:
YuCheng Hu 2022-06-09 01:24:30 -04:00
parent 46f9cae81e
commit 55e6b95222
2 changed files with 14 additions and 6 deletions

View File

@ -1,8 +1,8 @@
## Java Strings
## Java 字符串(Strings
This module contains articles about strings in Java.
### Relevant Articles:
### 相关文章
- [Use char[] Array over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords)
- [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string)
- [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty)
@ -13,3 +13,4 @@ This module contains articles about strings in Java.
- [Java Multi-line String](https://www.baeldung.com/java-multiline-string)
- [Guide to Java String Pool](https://www.baeldung.com/java-string-pool)
- [Fixing “constant string too long” Build Error](https://www.baeldung.com/java-constant-string-too-long-error)
- [Java 对象如何安全的 toString](https://www.ossez.com/t/java-tostring/14000)

View File

@ -9,17 +9,24 @@ import java.util.Objects;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
/**
* Java toString null Check
*
* <p><a href="https://www.ossez.com/t/java-tostring/14000">https://www.ossez.com/t/java-tostring/14000</a></p>
*
* @author YuCheng Hu
*/
public class StringNullCheckUnitTest {
@Test
public void toString_null_safe() {
Object obj = null;
String obj = null;
ObjectUtils.toString(obj, StringUtils.EMPTY);
ObjectUtils.toString(obj, ()->StringUtils.EMPTY);
ObjectUtils.toString(obj, () -> StringUtils.EMPTY);
assertThat("null").isEqualTo(Objects.toString(obj));
assertThat("").isEqualTo(Objects.toString(obj,StringUtils.EMPTY));
assertThat("").isEqualTo(Objects.toString(obj, StringUtils.EMPTY));
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->{
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> {
obj.toString();
});