UnnecessaryStubbingException

Getting this error when I run the test. The code works fine as I checked in debug mode though.
I tried using getStockQuote function from

  1. Using StockQuoteServiceFactory
  2. Using PortfolioManagerImpl

I also tried implementing the whole function in call() function in the implementation of Callable class.

Exception:

org.mockito.exceptions.misusing.UnnecessaryStubbingException:
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):

  1. -> at com.crio.warmup.stock.portfolio.PortfolioManagerPerformanceTest.getPortfolioManager(PortfolioManagerPerformanceTest.java:134)
  2. -> at com.crio.warmup.stock.portfolio.PortfolioManagerPerformanceTest.getPortfolioManager(PortfolioManagerPerformanceTest.java:136)
  3. -> at com.crio.warmup.stock.portfolio.PortfolioManagerPerformanceTest.getPortfolioManager(PortfolioManagerPerformanceTest.java:138)

Test File:

Mockito.doAnswer(invocation -> getCandles(aaplQuotes, false))
 .when(stockQuotesService).getStockQuote(eq("AAPL"), any(), any());
 Mockito.doAnswer(invocation -> getCandles(msftQuotes, withException))
  .when(stockQuotesService).getStockQuote(eq("MSFT"), any(), any());
 Mockito.doAnswer(invocation -> getCandles(googlQuotes, withException))
  .when(stockQuotesService).getStockQuote(eq("GOOGL"), any(), any());

Can you go through this link ?
@rg99https://forum.crio.do/t/org-mockito-exceptions-misusing-unnecessarystubbingexception/8514
This is occuring because of some mistakes in your code.

That’s what I mentioned @Piyush_Pant, everything is working fine in the code so I guess there shouldn’t be a mistake, and I tried replacing every piece of code to rule out the possibility of redundancy or missing use of any function.

Many of us has faced this error, and the conclusion was some logical mistake in our code, you can use breakpoints to trace the variables etc. @rg99

No mistakes in test , they are all good.

Also i added some lines in test files during testing …That also caused the same error . So please do not modify a line from test file. If you have modified than go to gitlab and take the original file from there

@Samyak_Jain, I don’t think I have made changes to any of the test files :frowning:
@Piyush_Pant If there had been a logical mistake in the code, then why do I see threads working fine, and returning the correct sorted outputs in debug mode (using breakpoints) ?

@Crio-Team I changed the full code structure, still the same error. Please help!

Hey sorry for the late response, As far as I can see , you have 2 cases failing,
I can give you a hint that,
One test Case: your Calculation logic is failing , make sure that you have sorted the data and calculated the respective annualized returns
https://forum.crio.do/t/extrapolated-annualized-return-test-case-fails/8062/2

Second test Case: Make sure you are re-throwing an exception rather than catching it.

Hey @rg99, As there is no response from your side on this topic for the last 13 hrs, Hoping that your issue is resolved, I am closing this topic.

If the issue still exists, you can create a new topic and continue the discussion there with this topic link in the description.