user3541321
user3541321

Reputation: 165

Powermock calling the real method instead of spy one

I am facing issue while mocking the private method with powermockito. Real method is getting called. I am trying to test a public method which calls private method with URI type parameter and returns String. I tried all the option but it always calling the real method due to that i am getting number format error.

package p;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.utils.URIBuilder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import com.wb.classics.handler.SearchInputInfo;
import org.powermock.api.support.membermodification.MemberMatcher.method;
import org.mockito.Matchers.anyString;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Test.class)
public class Test {


@Before
public void setUp() throws Exception {

    MockitoAnnotations.initMocks(this);
    // Create an object of a class under test.
    privateMethodClass = new PrivateMethodClass ();

}
@Test
public void Test() throws Exception {

    URI uri = null;
    URIBuilder builder = new URIBuilder();
    String MPM = "2000081";
    builder.setScheme("http").setHost("localhost").setPort(Integer.valueOf("8000")).setPath("/classic").setParameter("MPM", MPM);
    uri = builder.build();
    System.out.println("uri*********"+uri);
    String jsonstring ="jsonstring";

    PrivateMethodClass msspy = PowerMockito.spy(new PrivateMethodClass ());
    System.out.println("msspy***"+msspy);

    /*when(msspy, method(PrivateMethodClass .class,"privatemethod", URI.class))
    .withArguments(uri)
    .thenReturn(jsonstring);*/

    PowerMockito.doReturn(jsonstring).when(msspy,"privatemethod", uri);

     /*PowerMockito.doReturn(true).when(msspy, 
               method(MarkLogicServices.class, "privatemethod", URI.class))
                .withArguments(uri);*/

    String result= msspy.methodToTest(MPM);

    System.out.println("service****"+result);


}
}

and the class is

public PrivateMethodClass{

public String methodPublic()
{
 URI uri="";
 return methodprivate(uri);
}


private String methodprivate(URI uri)
 {
 return str;
}
}

pom.xml

 <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.1</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>

Below is the error message..

   java.lang.NumberFormatException: null
   at java.lang.Integer.parseInt(Unknown Source)
   at java.lang.Integer.valueOf(Unknown Source)
   at   com.wb.classics.handler.services.MarkLogicServices.fetchClassicByMPM(MarkLogicServices.java:132)
at com.wb.classics.handler.services.MarkLogicServicesTest.fetchClassicByMPMTest(MarkLogicServicesTest.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
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:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
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:86)
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:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Upvotes: 2

Views: 2739

Answers (2)

kuhajeyan
kuhajeyan

Reputation: 11017

Issue seems to be here is that, you are calling the method on spy instance, instead of mock.

As @Mureinik said

@RunWith(PowerMockRunner.class)
@PrepareForTest(PrivateMethodClass.class)
public class Test{.....

your test method

@Test
public void Test() throws Exception {

    URI uri = null;
    URIBuilder builder = new URIBuilder();
    String MPM = "2000081";
    builder.setScheme("http").setHost("localhost").setPort(Integer.valueOf("8000")).setPath("/classic").setParameter("MPM", MPM);
    uri = builder.build();
    System.out.println("uri*********"+uri);
    String jsonstring ="jsonstring";

    PrivateMethodClass ms = Mockito.mock(PrivateMethodClass.class);
    System.out.println("msspy***"+ms);


    PowerMockito.doReturn(jsonstring).when(msspy,"privatemethod", uri);     

    String result= ms.methodToTest(MPM);

    System.out.println("service****"+result);  



}

Upvotes: 0

Mureinik
Mureinik

Reputation: 310993

You need to annotate your test to enable PowerMockito to mock your class:

@RunWith(PowerMockRunner.class)
@PrepareForTest(PrivateMethodClass.class)
public class Test {
    // Test class' code...
}

Upvotes: 2

Related Questions