Skip to content

Commit a64b44e

Browse files
Solodyefmbenhassine
authored andcommitted
Add unit tests for ExecutionContext#get methods
Related to #718 Signed-off-by: Mahmoud Ben Hassine <mbenhassine@vmware.com>
1 parent 27adb97 commit a64b44e

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,24 @@
1515
*/
1616
package org.springframework.batch.item;
1717

18+
import java.io.Serializable;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.junit.jupiter.api.DisplayName;
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.util.SerializationUtils;
27+
1828
import static org.junit.jupiter.api.Assertions.assertEquals;
1929
import static org.junit.jupiter.api.Assertions.assertFalse;
2030
import static org.junit.jupiter.api.Assertions.assertNotEquals;
31+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2132
import static org.junit.jupiter.api.Assertions.assertNull;
2233
import static org.junit.jupiter.api.Assertions.assertThrows;
2334
import static org.junit.jupiter.api.Assertions.assertTrue;
2435

25-
import java.io.Serializable;
26-
27-
import org.junit.jupiter.api.Test;
28-
import org.springframework.util.SerializationUtils;
29-
3036
/**
3137
* @author Lucas Ward
3238
* @author Mahmoud Ben Hassine
@@ -196,4 +202,62 @@ public boolean equals(Object obj) {
196202

197203
}
198204

205+
@DisplayName("testGetByType")
206+
@Test
207+
void givenAList_whenGettingAccordingToListType_thenReturnCorrectObject() {
208+
// given - a list
209+
String key = "aListObject";
210+
List<String> value = List.of("value1", "value2");
211+
context.put(key, value);
212+
// when - getting according to list type
213+
@SuppressWarnings("unchecked")
214+
List<String> result = (List<String>) context.get(key, List.class);
215+
// then - return the correct list
216+
assertEquals(result, value);
217+
assertEquals(result.get(0), value.get(0));
218+
assertEquals(result.get(1), value.get(1));
219+
}
220+
221+
@DisplayName("testGetNullByDefaultParam")
222+
@Test
223+
void givenANonExistingKey_whenGettingTheNullList_thenReturnNull() {
224+
// given - a non existing key
225+
String key = "aListObjectButNull";
226+
// when - getting according to the key
227+
@SuppressWarnings("unchecked")
228+
List<String> result = (List<String>) context.get(key, List.class, null);
229+
List<String> result2 = (List<String>) context.get(key, List.class);
230+
// then - return the defined null list
231+
assertNull(result);
232+
assertNull(result2);
233+
}
234+
235+
@DisplayName("testGetNullByNotNullDefaultParam")
236+
@Test
237+
void givenAnNullList_whenGettingNullWithNonNullDefault_thenReturnDefinedDefaultValue() {
238+
// given - a non existing key
239+
String key = "aListObjectButNull";
240+
List<String> defaultValue = new ArrayList<>();
241+
defaultValue.add("value1");
242+
@SuppressWarnings("unchecked")
243+
// when - getting according to list type and default value
244+
List<String> result = (List<String>) context.get(key, List.class, defaultValue);
245+
// then - return defined default value
246+
assertNotNull(result);
247+
assertEquals(result, defaultValue);
248+
assertEquals(result.get(0), defaultValue.get(0));
249+
}
250+
251+
@DisplayName("testGetWithWrongType")
252+
@Test
253+
void givenAList_whenGettingWithWrongType_thenThrowClassCastException() {
254+
// given - another normal list
255+
String key = "anotherListObject";
256+
List<String> value = List.of("value1", "value2", "value3");
257+
context.put(key, value);
258+
// when - getting according to map type
259+
// then - throw exception
260+
assertThrows(ClassCastException.class, () -> context.get(key, Map.class));
261+
}
262+
199263
}

0 commit comments

Comments
 (0)