Mockito is providing a Empty restTemplate

I don’t know what’s going wrong, but looks like the restTemplate Mockito is providing is empty, which is causing NullPointException…

I’m confused, about what’s going on here? Been stuck on this issue for 2 days now. Help is appreciated.

The error message is as below:

A help from @anand-crio is much appreciated :slight_smile:

Faced same issue in Module 4, used a quick and dirty workaround. Tried using it here and code blew up. Now the Module 4 code works without the workaround too. But still stuck here on this issue only.

Its a NullPointer, probably the ideal scenario for debugger.
Put a breakpoint, and debug your test. Check what is coming null, then we will see why its coming null.

  1. What is null?
  2. Why its null?
  3. What can fix it?

Lets follow this approach, and try to answer the question one by one.

I tried putting breakpoint and evaluated every possible senario… but the line (inside TiingoService class) where I’m using restTemplate to exchange there, the member variable of the object restTemplate has all values equals to null, though the restTemplate is not null
So, what am I supposed to do here?

I really don’t know the 2nd question - Why its null? :slightly_frowning_face:

Whats your line#38 in TiingoService? The exception suggests that the error comes from TiingoService, and not RestTemnplate, so first we will have to inspect TiingoService code.
RestTemplate is not in the picture yet.

The debugger value of this.restTemplate in my code is not null but all its member variables are null, except one. But its still returning no result with the uri. However, when I initialise this.restTemplate with new RestTemplate() after it has already been initialised in constructor (possibly by Mockito), it works but fails next section of test that checks for getForObject.

What should I look for Sir?

That is line number 38, the exception is causing because of the restTemplate variable… as you can see in the debug console… all the member variables of restTemplate is null, but why is that?

and yes… as @shoryajain pointed out, instantiating new object does not causes Null Point exception but the further test cases failes.

I dont think having member variable null inside RestTemplate is a problem. There is something wrong in the call, where the API is throwing some exception… because of which, the response is coming as null…

RestTemplate#exchange is difficult to use, rather overkill in this case we are working on, and we dont recommend using it except advance use-cases where you need more control
Try using #getForObject

@anand-crio I am using getForObject only Sir. The issue remains same. The same call works when we reinitialise the restTemplate variable with its default constructor, but not when its getting it from tests.

What is the error you get in Module6?

Moreover sir, when using

restTemplate = new RestTemplate()

the member variables of restTemplate are not null in that case… so i guess, the null member variables here may cause error :frowning:

Null doesnt cause error in tests, read the test code, and you will realize that. We just had a session on Mockito. Apply it!

On running all tests, it throws an ArrayOutOfBounds exception. On running a debugging test, I found that the return array size is 0, which in turn is happening because the API call is not returning anything, which as I explained above, seems to be linked to RestTemplate.

the null pointer error is not due to mockito providing a null rest template but due to the test cases mocking the response of the restTemplate see

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

now the problem is getForObject has multiple variants

  1. getForObject(String url, Class responseType, Object… uriVariables)
  2. getForObject(URI url, Class responseType)
  3. getForObject(String url, Class responseType, Map<String, ?> uriVariables)

the mockito is only considered for variant number 1 but many of us might be using variant 2 instead which causes the problem.
variant 1 is confusing because the 3rd param need not receive anything not even null and most of us we don’t know this.

1 Like

So @aungrah do you mean to say that the test failed when you tried using second variation? Can you confirm that? I need to revisit the test. The test should support all variations of getForObject, proviided you are not passing any varargs in #1 :slight_smile:

yes if you use the URL wrapped by URI then it fails else it passes.
only the first variant is supported with no varargs

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

3 times the same thing is repeated in test file, maybe the changes to support all variants were forgot ?

Return Array size is 0, which means there are two possibilities -

  1. The mocking did not happen properly.
  2. You are doing something wrong in processing the reponse from Tiingo/Alphavantage.
    Put a breakpoint inside the function under test and debug through. Check if Mockito is returning correct data, if thats not the case, then you have a problem with stubbing, and you need to change the api call.
    If there is an issue processing the response, then its a different thing…

BTW, what happens to your code when alphavantage returns null/ empty response?

yes, you pointed it correctly, Its a test issue. Sorry for that. We need to correct it.

So in a nutshell, if you use getForObject(String, String.class) it will work. Not that bad, though!

I can still keep it this way to impose more learnings about mockito!! ( I am always right, ha ha… )

The return array size gets set to 0 because the response object is null and therefore the code section where return array is supposed to get its value from the response object is not executed. I ran a line by line debug test by adding breakpoint on every line and checked all variables. For some reason, the API call returns null and consequently the response object is null.

I haven’t started with alphavantage yet because I haven’t got that far. So I can’t say what may happen with it.