How to test switch case with method call and optional using Junit and Mockto

Viewed 128

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.

1 Answers

Here's example.

public class DataIdResolverTest {

    DataService dataService = Mockito.mock(DataService.class);
    private final DataIdResolver dataIdResolver = new DataIdResolver(dataService);


    @Test
    public void fetchDataIds_returnsListWithExceptedString_whenOptionalIsPresent() {
        //given
        ResponseData responseData = createYourResponseDataHere();
        Mockito.when(dataService.getData()).thenReturn(Optional.of(responseData));

        //when
        List<String> dataIds = dataIdResolver.fetchDataIds();

        //then
        //here you need to check if your dataIds have expected values, you can use Assertions
        //example
        Assert.assertEquals("expectedString", dataIds.get(0));
    }

    @Test
    public void fetchDataIds_returnsEmptyList_whenOptionalIsNotPresent() {
        //given
        ResponseData responseData = createYourResponseDataHere();
        Mockito.when(dataService.getData()).thenReturn(Optional.empty());

        //when
        List<String> dataIds = dataIdResolver.fetchDataIds();

        //then
        Assert.assertTrue(dataIds.isEmpty());
    }
}

In general read more about unit tests. You want test class DataIdResolver but you didn't even create instance of it. You can't test private method without reflection, in general it's bad practice to do it. Also remember to name your test methods better, you can read about it here https://dzone.com/articles/7-popular-unit-test-naming

Related