Powermock调用真正的方法而不是间谍方法

时间:2016-07-29 09:00:14

标签: java junit powermockito

我在使用powermockito模拟私有方法时遇到问题。 真正的方法被调用。 我试图测试一个公共方法,它使用URI类型参数调用私有方法并返回String。我尝试了所有的选项,但它总是调用真正的方法,因为我得到数字格式错误。

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);


}
}

,班级是

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>

以下是错误消息..

   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)

2 个答案:

答案 0 :(得分:2)

您需要注释测试以启用PowerMockito来模拟您的课程:

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

答案 1 :(得分:0)

问题似乎在于,您在spy实例上调用方法,而不是模拟。

正如@Mureinik所说

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

您的测试方法

@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);  



}