TiingoServiceTest Fails when restTemplate is passed and constructor is not used in TiingoService

In tiingo Service

protected TiingoService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
This is used

java.lang.NullPointerException

protected TiingoService(RestTemplate restTemplate) {
this.restTemplate = new RestTemplate ();
}

This is used

In tiingo Service

assertTrue(!propertyKeyValues.isEmpty() || !propertyKeyValues2.isEmpty() || !propertyKeyValues3.isEmpty());

This assertion Fails

restTemplate constructor is not used in test file .

I think my object initialization is missing when passing normal

In case i use new constructor at service its a new instance hence

List propertyKeyValues = urlCaptor.getAllValues();
List propertyKeyValues2 = urlCaptorWithMap.getAllValues();
List propertyKeyValues3 = urlCaptorAsUri.getAllValues();

These values are not set .

Help ?

do not tamper with supplied restTemplate value. Try to debug and perform appropriate Exception Handling instead.

1 Like

Exception is occuring because of rest Template value , Without the given value it works fine .
I will apply the exception handling as u suggested .

Why adding this line of code in Tiingo Service

RestTemplate restTemplate = new RestTemplate();

runs sucessful but assertTrue(!propertyKeyValues.isEmpty() || !propertyKeyValues2.isEmpty() || !propertyKeyValues3.isEmpty()); Fails !!!

I tired Exception Handling it doesn’t work

String str = restTemplate.exchange(site,
HttpMethod.GET, entity, String.class).getBody();

Throws NullPointerException

Using restTemplate.getForObject Solved the issue
But can anyone point out why this sort of behavior was seen when using exchange .

Because this code tests if you used the rest template supplied and called any of its method. Since you aren’t using the supplied template it’s failing.

Go through this link

Faced the exact issue as you are facing.

Yeah that’s an issue. I faced it too. Check out the link I shared. Crio mentors were very helpful in resolving it.

Thnx I think i got the idea for the issue it was a funny way to test based on method used.