getStockQuote does not get called!

Hi there, I am facing a very nerve cracking absurd problem I ever have seen. The funciton getStockQuote is not getting called from calculateAnnualizedReturn. How do I know this?

  1. I am returning empty List from getStockQuote but while running the calculateExtrapolatedAnnualizedReturn test it returns some value(which is wrong) to calculateAnnualizedReturn funciton.
  2. While debugging I put breakpoints in getStockQuote function but the debugger never stops while running the calculateExtrapolatedAnnualizedReturn test.

Here are some images.

As seen in last screenshot which is of actually calling the Tiingo Service from browser the Open price(=267.78) is different from what getStockQuote returned(=144.53, In last second screenshot).

referring to your first image ;…you are calling geturl, so are you sure it is containing the API result. And one more thing you haven’t written proper code in getStockQuote…do note that you didn’t use the result anywhere at all…You get the result from API and you need to store it in List<Candle> then return the same from getStockQuote

And in the second image…you are converting date to string which you don’t have to.

are you calling the correct getStockQuote() function, check calculateannualised function. you can use the call stack to know where your control is going

Anurag it’s not wrong you will get to know when you move forward in the later modules. So have patience and only work on passing the test case.

You can see the size of candle in the test it is only meant for testing purposes the test file uses string json which is already defined in the test case as you can see.

Hi @AmoghaKS, so my geturl fuction gives me the url as string by replacing placeholders in the standard Tiinker api call. Then I pass this url to getCollection function which gives me the actual result(List) but i have commented the getCollection funciton as you can see in the first figure for testing getStockQuote function. I am using this getStockQuote funciton in calculateAnnualizedReturn function so nothing is redundant or useless code here.
The whole point is that if I am returning empty list in getStockQuote how can the stocks variable in calculateAnnualizedReturn function have 3 values. Please understand the question properly.

@Jitender, the test calculateExtrapolatedAnnualizedReturn is failing.

What do you mean by correct getStockQuote function? You can see in the second screenshot how I have called the function. Kindly tell me if it is wrong.

You have to use the first index of candle list to get open value and last index to collect close valuee. I am sure that much hints are more than enough to pass this test.

@anurag1 nothing is wrong here you have to use the whole getstocks implementation function seperate in the next module. Be focused to implement the logic that’s it

@anurag1 I am not going to tell you how to call getstocks function at least define a data type of stocks properly.
And also don’t change the return type of getstocks you don’t have to do that

@Jitender, thank you. The test case passed but I will still wait for some explanation for this behavior before closing the issue.

Haha sure will do but you have to wait a little.

Hey, I can see that this query of yours has been resolved by an answer given by @anurag1, Shall i close this issue or you need more insights on it?

@Sakshat-Crio.Do I need more insights please

Also no satisfying answer is provided yet.

Hi @anurag1, This is not actually an absurd behavior. This happens due to Mockito being configured to do so when you are running the JUnit tests.

if (moduleToRun.equals("REFACTOR")) {
          .when(portfolioManager).getStockQuote(eq("AAPL"), any(), any());
          .when(portfolioManager).getStockQuote(eq("MSFT"), any(), any());
          .when(portfolioManager).getStockQuote(eq("GOOGL"), any(), any());

Take a look at the above code from Here, Mockito is told that if getStockQuote is called with AAPL, MSFT or GOOGL symbol then return the respective dummy candle. So in reality when your test runs the getStockQuote function is never executed only the dummy values are returned by Mockito when it finds any to reduce the external dependence of the test, this results in the test not breaking when external data changes.

You can read more about Mockito here.

1 Like

Because of this I’m getting Assertion error. Is there a way to get around it? @dotslash21

Yeah put breakpoints in the test function(most probably the assertion error is thrown from here only) and check the value your function returned and the actual value expected. One mistake I made was pointed out in this comment.

1 Like

Thanks. It got resolved!