PowerMockito.doReturn returns null(PowerMockito.doReturn 返回 null)
问题描述
这是我正在测试的课程:
This is my class under test:
public class A {
public Integer callMethod(){
return someMethod();
}
private Integer someMethod(){
//Some Code
HttpPost httpPost = new HttpPost(oAuthMessage.URL);
//Some Code
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost); ------1
Integer code = httpResponse.getStatusLine().getStatusCode(); ---2
return code;
}
现在我想模拟第 1 行 &2&返回一个模拟 HttpResponse &代码.
Now I want to mock the line 1 & 2 & return a mock HttpResponse & code.
我试过这个但失败了:
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.crypto.*")
public class TestA {
//Spying some things here & Injecting them
@Test
public void testA() {
DefaultHttpClient defaultHttpClientMock = PowerMockito.mock(DefaultHttpClient.class);
HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class, RETURNS_DEEP_STUBS);
HttpClient httpClient = PowerMockito.mock(HttpClient.class);
//HttpResponse httpResponseMock PowerMockito.mock(HttpResponse.class);
HttpPost httpPost = PowerMockito.mock(HttpPost.class);
PowerMockito.whenNew(DefaultHttpClient.class).withNoArguments().thenReturn(defaultHttpClientMock);
PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost); //Returns null. It never returns httpResponse.
PowerMockito.when(httpResponse.getStatusLine().getStatusCode()).thenReturn(anyInt());
//call the method
}
PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost)
总是返回 null.我希望它返回 HttpResponse
的模拟对象.我已阅读与此错误相关的其他帖子,但不确定在我的情况下该怎么做.有人可以帮忙吗?
PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost)
always returns null. I want it to return the mock object of HttpResponse
.
I have read other posts related to this error but not sure what to do in my case. Can anyone help?
推荐答案
代替
PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost);
你应该使用
PowerMockito.when(httpResponse.execute(httpPost)).thenReturn(httpResponse);
您的测试中还有一些问题:不正确的模拟构造函数,您根本不需要 httpResponse.
You also have some problems in your test : incorrect mocking constructor and you don't need httpResponse at all.
更新此代码对我来说可以正常工作:
Update This code works correctly to me:
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.crypto.*")
@PrepareForTest({ HttpPost.class, DefaultHttpClient.class, A.class })
public class TestA {
@Test
public void testA() throws Exception {
HttpPost httpPost = Mockito.mock(HttpPost.class);
PowerMockito.whenNew(HttpPost.class).withArguments(oAuthMessage.URL).thenReturn(httpPost);
DefaultHttpClient defaultHttpClientMock = PowerMockito.mock(DefaultHttpClient.class);
HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
PowerMockito.whenNew(DefaultHttpClient.class).withNoArguments().thenReturn(defaultHttpClientMock);
PowerMockito.when(defaultHttpClientMock.execute(httpPost)).thenReturn(httpResponse);
StatusLine statusLine = PowerMockito.mock(StatusLine.class);
PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
Integer expected = new Integer(0);
PowerMockito.when(statusLine.getStatusCode()).thenReturn(expected);
A a = new A();
Assert.assertEquals(expected, a.callMethod());
}
}
这篇关于PowerMockito.doReturn 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PowerMockito.doReturn 返回 null
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01