gpt4 book ai didi

java - Powermock 调用真正的方法而不是 spy 方法

转载 作者:行者123 更新时间:2023-11-30 02:51:01 24 4
gpt4 key购买 nike

我在使用 powermockito 模拟私有(private)方法时遇到问题。真正的方法正在被调用。我正在尝试测试一个公共(public)方法,该方法使用 URI 类型参数调用私有(private)方法并返回 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);


}
}

类(class)是

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)

最佳答案

您需要注释您的测试以使 PowerMockito 能够模拟您的类:

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

关于java - Powermock 调用真正的方法而不是 spy 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38654679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com