Merge pull request #9451 from ricardomoreirab/master
[BAEL-4136] What is this: [Ljava.lang.Object;?
This commit is contained in:
commit
d68d458763
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.arrays;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class JavaArraysToStringUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInstanceOfArray_whenTryingToConvertToString_thenNameOfClassIsShown() {
|
||||||
|
Object[] arrayOfObjects = { "John", 2, true };
|
||||||
|
assertTrue(arrayOfObjects.toString().startsWith("[Ljava.lang.Object;"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInstanceOfArray_whenUsingArraysToStringToConvert_thenValueOfObjectsAreShown() {
|
||||||
|
Object[] arrayOfObjects = { "John", 2, true };
|
||||||
|
assertEquals(Arrays.toString(arrayOfObjects), "[John, 2, true]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInstanceOfDeepArray_whenUsingArraysDeepToStringToConvert_thenValueOfInnerObjectsAreShown() {
|
||||||
|
Object[] innerArray = { "We", "Are", "Inside" };
|
||||||
|
Object[] arrayOfObjects = { "John", 2, innerArray };
|
||||||
|
assertEquals(Arrays.deepToString(arrayOfObjects), "[John, 2, [We, Are, Inside]]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInstanceOfDeepArray_whenUsingStreamsToConvert_thenValueOfObjectsAreShown() {
|
||||||
|
Object[] arrayOfObjects = { "John", 2, true };
|
||||||
|
List<String> listOfString = Stream.of(arrayOfObjects)
|
||||||
|
.map(Object::toString)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertEquals(listOfString.toString(), "[John, 2, true]");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user