TiingoServiceTest is failing

TiingoServiceTest is showing indexOutOfBounds Exception at line number 65 due to empty List<Candle> obtained.

Analysis :
According to this code :

Mockito.doReturn(sampleTiingoResponse) .when(restTemplate).getForObject(anyString(), eq(String.class));

sampleTiingoResponse should be returned when getForObject method is called.

But when this code runs
List<Candle> candles = tiingoService .getStockQuote("GOOGL", LocalDate.parse("2019-01-01"), LocalDate.parse("2019-01-04"));

The candles object is getting emptyList value, which should not be the case.
Also TiingoService class has getForObject called inside getStockQuote method.
Please provide some insight why this test is failing. Which point I am missing?

You might be recieving null values from response while calling the api thats why this test throws OutofboundException

@yakshit I know that.
While calling the API the value received would be null only because the actual API call is not made.
Also, this code Mockito.doReturn(sampleTiingoResponse).when(restTemplate).getForObject(anyString(), eq(String.class));
ensures that whenever the getForObject method is called it should return ‘sampleTiingoReponse’
Which is not happening. I wan’t to know the reason why?

It depends with which class you are mapping the response object with.
As you can see the above mockito code.
It is expecting to be mapped by String.class
Are you mapping by String.class?

I am using TiingoCandle[ ].class to map the response object.

I was expecting this.
Because in this module the Mockito is expecting the response to be mapped with String class.
You can map using TiingoCandle[].class once again after the response is mapped with string.
So the simple solution is map response with string.class
After that you can map the stored string response on variable again with TiingoCnadle.class.
I hope it helps

1 Like