Jaykumar Jaykumar - 3 months ago 20
Java Question

Parsing in Mockito

I am using Mockito in My JUnit to mock some Objects. The code works fine but when it gets to a point where a setter() parses a String into a Integer I get

NumberFormatException
which is fine but I have one rule and it covers all the lines but when it gets to this setter it throws exception.

Scenario 1 MUT

if( request.getParameter(UiIntegrationKeyConstants.UI_INTEGRATION_LINE_OF_BUSINESS) != null ) {
config.setLob((request.getParameter(UiIntegrationKeyConstants.UI_INTEGRATION_LINE_OF_BUSINESS)));
}


JUnit for it

Mockito.when(request.getParameter(Mockito.any(String.class))).thenReturn(UiIntegrationKeyConstants.UI_INTEGRATION_CLIENT_CONTEXT_PATH);


Above rule works

Scenario 2 MUT

if( request.getParameter(UiIntegrationKeyConstants.UI_INTEGRATION_CREATED_DAYS) != null ) {
config.setCreatedDays(Integer.parseInt(request.getParameter(UiIntegrationKeyConstants.UI_INTEGRATION_CREATED_DAYS).toString()));
}


So for scenario 2, I get exception. I am not sure how this would work.

Failure Trace:

java.lang.NumberFormatException: For input string: "clientContextPath"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at com.mercuryinsurance.esignature.integration.interceptor.ESignatureIntegrationInterceptor.intercept(ESignatureIntegrationInterceptor.java:143)
at test.com.mercuryinsurance.esignature.integration.interceptor.TestESignatureIntegrationInterceptor.testIntercept2(TestESignatureIntegrationInterceptor.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


Please let me know if I am doing anything wrong.

Thanks,

Answer

I the following

Mockito.when(request.getParameter(Mockito.any(String.class))).
    thenReturn(UiIntegrationKeyConstants.UI_INTEGRATION_CLIENT_CONTEXT_PATH);

is the only when(), then all getParameter()calls will return that string. If you want to specify a special String, which can be parsed to an Integer, you can use

Mockito.when(request.getParameter(Mockito.eq("key"))).
    thenReturn("4711");