Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 2b776b3

Browse files
Added a test to confirm that builders correctly support null elements in collections
1 parent eedff5b commit 2b776b3

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

kotlin-builder-example-usage/src/main/kotlin/com/thinkinglogic/example/CollectionsDataClass.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.util.*
88
@Builder
99
data class CollectionsDataClass(
1010
val listOfStrings: List<String>,
11+
@NullableType val listOfNullableStrings: List<String?>,
1112
val setOfLongs: Set<Long>,
1213
@NullableType val setOfNullableLongs: Set<Long?>,
1314
val hashSet: HashSet<Long>,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thinkinglogic.example;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.time.LocalDate;
6+
import java.util.*;
7+
8+
import static java.util.Arrays.asList;
9+
import static java.util.Collections.emptyList;
10+
import static java.util.Collections.emptySet;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.assertj.core.api.Assertions.catchThrowable;
13+
import static org.assertj.core.api.BDDAssertions.then;
14+
15+
public class CollectionsDataClassJavaTest {
16+
17+
@Test
18+
void builderShouldAllowNullValuesInCollections() {
19+
// given
20+
CollectionsDataClassBuilder builder = new CollectionsDataClassBuilder();
21+
22+
List<String> nullableList = asList(null, null, "foo");
23+
Set<Long> nullableSet = new HashSet<>(asList(null, null, 1L));
24+
25+
HashMap<String, LocalDate> nullableMap = new HashMap<>();
26+
nullableMap.put("Foo", null);
27+
28+
29+
// when
30+
CollectionsDataClass result = builder
31+
.listOfNullableStrings(nullableList)
32+
.mapOfStringToNullableDates(nullableMap)
33+
.setOfNullableLongs(nullableSet)
34+
.collectionOfDates(emptySet())
35+
.listOfStrings(emptyList())
36+
.setOfLongs(emptySet())
37+
.hashSet(new HashSet<>())
38+
.treeMap(new TreeMap<>())
39+
.build();
40+
41+
// then
42+
assertThat(result.getListOfNullableStrings()).isEqualTo(nullableList);
43+
assertThat(result.getMapOfStringToNullableDates()).isEqualTo(nullableMap);
44+
assertThat(result.getSetOfNullableLongs()).isEqualTo(nullableSet);
45+
}
46+
}

kotlin-builder-example-usage/src/test/kotlin/com/thinkinglogic/example/CollectionsDataClassTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal class CollectionsDataClassTest {
1414
hashSet = HashSet(setOf(1L, 2L)),
1515
collectionOfDates = listOf(LocalDate.now()),
1616
listOfStrings = listOf("Foo", "bar"),
17+
listOfNullableStrings = listOf("Foo", "bar", null, null),
1718
setOfLongs = setOf(3L, 4L),
1819
setOfNullableLongs = setOf(3L, null),
1920
treeMap = TreeMap(),
@@ -25,6 +26,7 @@ internal class CollectionsDataClassTest {
2526
.hashSet(expected.hashSet)
2627
.collectionOfDates(expected.collectionOfDates)
2728
.listOfStrings(expected.listOfStrings)
29+
.listOfNullableStrings(expected.listOfNullableStrings)
2830
.setOfLongs(expected.setOfLongs)
2931
.setOfNullableLongs(expected.setOfNullableLongs)
3032
.treeMap(expected.treeMap)

0 commit comments

Comments
 (0)