[Module 6][QMoney][AlphavantageServiceTest]: Need help with Mockito tests in AlphavantageServiceTest.java file

I see that the getStockSingleQuote has ArgumentCaptor related tests and I am facing difficulty in understanding these tests, Could someone explain the design and intention of these tests, also, how these work.

Thanks

Hi @praveen.sunkara
Looks like you are facing challenges in the current module.

Below is a link to all the FAQ’s for the current module

Below are a few similar topics raised by other users which you might find usefull

Note - This is a BETA Feature,if you find this helpful, kindly let us know.
Feedback link - https://forms.gle/cmxN5pdTXgnwgC986

Kindly mark this as a solution if this has helped you solve your query

hello @praveen.sunkara
Great that you ask, let me give you a brief intro of all the keywords present in the test you mentioned.

  1. mockito - mockito is a very strong tool for testing.As the name suggests it is used to mock the response of a API call, it is obvious that you cannot call API everytime for testing purposes, so we use mockito which provides us the functionality that we can define what to return when a particular request is made.

  2. Argumentcaptor - As the name suggests it is used to catch arguments passes during function calls, it basically checks that are you passing the correct argument while making the function call.

Now that you have a high level overview of both things, You can easily make out what the test is asking and how it is designed, please go through it and if you face any issues feel free toreply to this thread and if this solves your issue, mark it as solution.

ArgumentCaptor urlCaptor = ArgumentCaptor.forClass(String.class);
Mockito.verify(restTemplate, atLeast(0)).getForObject(urlCaptor.capture(), any());

In the above first line, I do not quite understand what “forClass(String.class)” is supposed to do.

What I do understand is that we are creating an object of the type ArgumentCaptor and this object will provide us with methods to capture the arguments passed to other functions (please correct me here, if it needs correction).

Now, on the second line we are trying to get a mocked response by invoking the getForObject method of the mocked restTemplate object although I am not sure what atLeast(0) is doing here.
In the getForObject method, we are trying to capture any type of argument.
I want to understand what response is captured over here and also what is the request we are making/mocking?

Thanks

hello @praveen.sunkara
the first line meant that you are capturing argument which belong to string class, i.e. the argument of the mocked method is string

the second line is basically to check that the “getforobject” of the resttemplate is being called. This type of test is to ensure that you are not hardcoding values and actually calling the API. becasue only when you call the API the method getforobject is being used, hence verify.

But the restTemplate object has all values as null so the response will also be null and we are checking the below assertion to return true, how could this be possible?

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

how is the value is null,
see mockito come into picture at the very laast step, i.e. when this statement execute
resttemplate.getforobject(x,y)
after this statement mockito will come into picture and return the aaplquotes, that means args and variables are all poplulated , only and only the actual API calling is not happening,everything is happening as usual.
Hence if everything is happening as usual then out of the given 3 any one should be non empty.

hello @praveen.sunkara
The parameters that you should look for while debug is
“urlCaptor.getAllValues()”
“urlCaptorAsmap.getAllValues()”
“urlCaptorAsUri.getAllValues()”
becauase these parameters store the numbers of arguments you are passing.

Now coming back to this test failing, there can be various reasons behind it.

  1. You are not using the desired method call and hence there is no argument in the first place.
  2. There are global variables in the class file for which your test is failing, all the tests run and execute wrt to the global variables that means if you define your own local variable of that, the instance of the global variable which all tests are using will eventually hit the deadend since you are not using the global variable.

Can you see in the debug window, there is supposed to be*(ArrayList@70 size=1*)(Second line,Its my code and the debug is supposed to look like this)
if you do debug in your codebase and find value of it you are getting 0, which is wrong.

Can you find your mistake now?

Hey @Anmol-Crio_TA, I did not change anything in the tests, Am I suppose to make changes?
I am still getting ArrayList size as 0 for all three argument captors.
Can you send me the screenshot with the lines of code where ArgumentCaptor is used?

Thanks

No, your tests are fine, I debugged the correct code and got the debug window as shown in the pic,
if you are not getting it then there is something incorrect in your codbase.If it have been correct you would get 1 for urlcaptror.getallvalues()

and argumentcaptor is used the goforobject in Alphavantageservice.java, for more insights cand finding about your test failing consider the points I have mentioned in the earlier reply.

Hey @Anmol-Crio_TA, I am not able to figure this out. Can you post the screenshot where you have the ArgumentCaptor lines of code?

I think this issue is closed.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.