https://www.ossez.com/t/java-tostring/14000 Java 对象如何安全的 toString
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.chararraypassword;
|
||||
package com.ossez.chararraypassword;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
|
||||
|
||||
public class StringNullCheckUnitTest {
|
||||
@Test
|
||||
public void toString_null_safe() {
|
||||
Object obj = null;
|
||||
|
||||
ObjectUtils.toString(obj, StringUtils.EMPTY);
|
||||
ObjectUtils.toString(obj, ()->StringUtils.EMPTY);
|
||||
assertThat("null").isEqualTo(Objects.toString(obj));
|
||||
assertThat("").isEqualTo(Objects.toString(obj,StringUtils.EMPTY));
|
||||
|
||||
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->{
|
||||
obj.toString();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.localization;
|
||||
package com.ossez.localization;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.multiline;
|
||||
package com.ossez.multiline;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.randomstrings;
|
||||
package com.ossez.randomstrings;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Test;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.stringnotempty;
|
||||
package com.ossez.stringnotempty;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.stringpool;
|
||||
package com.ossez.stringpool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.stringtoolong;
|
||||
package com.ossez.stringtoolong;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
Reference in New Issue
Block a user