Skip to content

Commit

Permalink
Add unit test for the ExecutionContext.get method added in Issue spri…
Browse files Browse the repository at this point in the history
  • Loading branch information
Solodye committed Sep 24, 2024
1 parent 53e33c0 commit debb30b
Showing 1 changed file with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
*/
package org.springframework.batch.item;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.util.SerializationUtils;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author Lucas Ward
* @author Mahmoud Ben Hassine
Expand Down Expand Up @@ -196,4 +196,52 @@ public boolean equals(Object obj) {

}

@DisplayName("testGetByType")
@Test
void givenAList_whenGettingAccordingToListType_thenReturnCorrectObject() {
// given - a list
String key = "aListObject";
List<String> value = List.of("value1", "value2");
context.put(key, value);
// when - getting according to list type
@SuppressWarnings("unchecked")
List<String> result = (List<String>) context.get(key, List.class);
// then - return
assertEquals(result, value);
assertEquals(result.get(0), value.get(0));
assertEquals(result.get(1), value.get(1));
}

@DisplayName("testGetNullByDefaultParam")
@Test
void givenAnNullList_whenGettingTheNullList_thenReturnNull() {
// given - a null list
String key = "aListObjectButNull";
List<String> value = null;
context.put(key, value);
// when - getting according to the key
@SuppressWarnings("unchecked")
List<String> result = (List<String>) context.get(key, List.class, null);
// then - return the defined null list
assertNull(result);
}

@DisplayName("testGetNullByNotNullDefaultParam")
@Test
void givenAnNullList_whenGettingNullWithNonNullDefault_thenReturnDefinedDefaultValue() {
// given - a null list and expected default value
String key = "aListObjectButNull";
List<String> value = null;
List<String> defaultValue = new ArrayList<>();
defaultValue.add("value1");
context.put(key, value);
@SuppressWarnings("unchecked")
// when - getting according to list type and default value
List<String> result = (List<String>) context.get(key, List.class, defaultValue);
// then - return defined default value
assertNotNull(result);
assertEquals(result, defaultValue);
assertEquals(result.get(0), defaultValue.get(0));
}

}

0 comments on commit debb30b

Please sign in to comment.