Getting wrong output while calculation is correct

  • com.crio.warmup.stock.PortfolioManagerApplicationTest
  • mainCalculateAnnualReturn





Stack trace:
org.opentest4j.AssertionFailedError: expected: <0.814> but was: <0.028356633217136773>
	at org.junit.jupiter.api.AssertionUtils.failNotEqual(
	at org.junit.jupiter.api.AssertEquals.assertEquals(
	at org.junit.jupiter.api.AssertEquals.assertEquals(
	at org.junit.jupiter.api.Assertions.assertEquals(
	at com.crio.warmup.stock.PortfolioManagerApplicationTest.mainCalculateAnnualReturn(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(

total_returns = (current_price - purchase_price) / purchase_price

annualized_returns = (1 + total_returns) ^ (1 / total_num_years) - 1

Check whether u used the same formula above. And calculate for no of days instead of years

I am doing the same thing…
I have passed testCalculateAnnualizedReturn and testCalculateAnnualizedReturnGoogl
so the calculation must be correct…

The assertion error is due to logic error. Once recheck the logic

I am stuck on this for a long time… I have checked everything I could… I am totally confused now what to do…

Hi @bindalmukul99,

As you are passing the tests for the calculateAnnualizedReturn() method, I would suggest debugging the mainCalculateAnnualReturn test.

  • You can use postman to make the API call you are making inside mainCalculateSingleReturn method for the arguments given in the test

  • See if it falls into the case where the stock data for the provided endDate isn’t available. If so, choose the stock data for the date closest to the endDate available

  • Try to calculate manually for the first stock the values as you debug through the calculateAnnualizedReturns - you can use the step into feature when debugging to go inside method calls if you have only placed breakpoints in the test file

Let me know if it helped :slight_smile:

1 Like

@bindalmukul99 Adding to the points said by @nabhanpv ,Firstly try to calculate the number of days between the required dates and then convert that into years, in the process of calculating the AnnualizedReturns .

Ok I will try. Thanks.

I’m closing this topic due to no response from anyone since last 15 hours. If your query is resolved you can mark any reply as a solution. If you still have the query, please create a new topic paste the link of this topic in the description.