I have to test DataIdResolver class and it is a class that has a switch case and optional in it. Also there is a one case and default part of switch case that contains each method call. I need to test it and I am not sure how to do it.
public class DataIdResolver {
final DataService dataService;
public DataIdResolver(DataService dataService) {
this.dataService = dataService;
}
public List<String> fetchDataIds(String dataId){
Optional<DataResponse> optionalResponse = dataService.getData(dataId);
if(optionalResponse.isPresent()) {
switch(optionalResponse.get().author){
case "Harry Potter":
return resolveAuthorDataIdAndSiblingsAuthorDataId(optionalResponse.get());
default:
return resolveServerSideDataIdAndSiblingsServerSideDataId(optionalResponse.get());
}
} else {
return List.of(dataId);
}
}
private List<String> resolveAuthorDataIdAndSiblingsAuthorDataId(DataResponse response) {
List<String> resolvedServerSideIds = new ArrayList<>();
resolvedServerSideIds.add(response.serverSideDataId);
if (!response.siblings.isEmpty()){
for (SiblingsDataResponse s : response.siblings) {
resolvedServerSideIds.add(s.dataId);
}
}
return resolvedServerSideIds;
}
}
Here is what I have for start:
public class DataIdResolverTest {
DataService dataService = Mockito.mock(DataService.class);
RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
private final HttpDataService httpDataService = new HttpDataService(restTemplate);
@Test
public void fetchDataIds() {
Optional<DataResponse> optionalResponse = dataService.getData(dataId);
optionalResponse.ifPresent(dataResponse -> Mockito
.when(!dataResponse.siblings.isEmpty()).thenReturn(Boolean.valueOf("siblingsDataId")));
httpDataService.getData("dataIdTest");
}
@Test
public void resolveAuthorDataIdAndSiblingsAuthorDataId(){
}
}
Should I test it all in one test method? What is the best way to do it?
Any advice appreciated.