Getting wrong output while calculation is correct

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

Failed

2390ms

Message:

N/A

Stack trace:
org.opentest4j.AssertionFailedError: expected: <0.814> but was: <0.028356633217136773>
	at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
	at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
	at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:86)
	at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:81)
	at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:993)
	at com.crio.warmup.stock.PortfolioManagerApplicationTest.mainCalculateAnnualReturn(PortfolioManagerApplicationTest.java:56)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hier

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.