|
| 1 | +package com.baeldung.listofobjectstolistofstring; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | + |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +public class ConvertObjectListToStringListUnitTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void givenObjectList_whenForEachUsedToConvert_thenReturnSuccess() { |
| 17 | + List<String> outputList = new ArrayList<>(objectListWithNull().size()); |
| 18 | + for (Object obj : objectListWithNull()) { |
| 19 | + outputList.add(Objects.toString(obj, null)); |
| 20 | + } |
| 21 | + Assert.assertEquals(expectedStringListWithNull(), outputList); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void givenObjectList_whenUsingStreamsToConvert_thenReturnSuccess() { |
| 26 | + List<String> outputList; |
| 27 | + outputList = objectListWithNull().stream() |
| 28 | + .map((obj) -> Objects.toString(obj, null)) |
| 29 | + .collect(Collectors.toList()); |
| 30 | + Assert.assertEquals(expectedStringListWithNull(), outputList); |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenObjectList_whenUsingStreamsUnmodifiableListToConvert_thenReturnSuccess() { |
| 36 | + List<String> outputList; |
| 37 | + outputList = objectListWithNull().stream() |
| 38 | + .filter(Objects::nonNull) |
| 39 | + .map((obj) -> Objects.toString(obj, null)) |
| 40 | + .collect(Collectors.toUnmodifiableList()); |
| 41 | + Assert.assertEquals(expectedStringListWithoutNull(), outputList); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenObjectList_whenUsingGuavaTransform_thenReturnSuccess() { |
| 47 | + List<String> outputList; |
| 48 | + outputList = Lists.transform(objectListWithNull(), obj -> Objects.toString(obj, null)); |
| 49 | + Assert.assertEquals(expectedStringListWithNull(), outputList); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenObjectListWithNoNull_whenUsingToList_thenReturnSuccess() { |
| 54 | + List<String> outputList; |
| 55 | + outputList = objectListWithoutNull().stream() |
| 56 | + .map((obj) -> Objects.toString(obj, null)) |
| 57 | + .toList(); |
| 58 | + Assert.assertEquals(expectedStringListWithoutNull(), outputList); |
| 59 | + } |
| 60 | + |
| 61 | + private List<String> expectedStringListWithNull() { |
| 62 | + List<String> listOfStrings = new ArrayList<>(); |
| 63 | + listOfStrings.add("1"); |
| 64 | + listOfStrings.add("true"); |
| 65 | + listOfStrings.add("hello"); |
| 66 | + listOfStrings.add(Double.toString(273773.98)); |
| 67 | + listOfStrings.add(null); |
| 68 | + listOfStrings.add(new Node(2, 4).toString()); |
| 69 | + listOfStrings.add(new User("John Doe").toString()); |
| 70 | + return listOfStrings; |
| 71 | + } |
| 72 | + |
| 73 | + private List<Object> objectListWithNull() { |
| 74 | + List<Object> listOfStrings = new ArrayList<>(); |
| 75 | + listOfStrings.add(1); |
| 76 | + listOfStrings.add(true); |
| 77 | + listOfStrings.add("hello"); |
| 78 | + listOfStrings.add(Double.valueOf(273773.98)); |
| 79 | + listOfStrings.add(null); |
| 80 | + listOfStrings.add(new Node(2, 4)); |
| 81 | + listOfStrings.add(new User("John Doe")); |
| 82 | + return listOfStrings; |
| 83 | + } |
| 84 | + |
| 85 | + private List<String> expectedStringListWithoutNull() { |
| 86 | + return List.of("1", "true", "hello", Double.toString(273773.98), new Node(2, 4).toString(), new User("John Doe").toString()); |
| 87 | + } |
| 88 | + |
| 89 | + private List<Object> objectListWithoutNull() { |
| 90 | + return List.of(1, true, "hello", Double.valueOf(273773.98), new Node(2, 4), new User("John Doe")); |
| 91 | + } |
| 92 | +} |
0 commit comments