PowerMockito.doReturn返回null

问题描述

代替

PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost);

你应该使用

  1. PowerMockito.when(httpResponse.execute(httpPost)).thenReturn(httpResponse);

您的测试中还存在一些问题:模拟构造函数不正确,并且根本不需要httpResponse。

代码对我而言正常工作:

  1. @RunWith(PowerMockRunner.class)
  2. @PowerMockIgnore("javax.crypto.*")
  3. @PrepareForTest({ HttpPost.class, DefaultHttpClient.class, A.class })
  4. public class TestA {
  5. @Test
  6. public void testA() throws Exception {
  7. HttpPost httpPost = Mockito.mock(HttpPost.class);
  8. PowerMockito.whenNew(HttpPost.class).withArguments(oAuthMessage.URL).thenReturn(httpPost);
  9. DefaultHttpClient defaultHttpClientMock = PowerMockito.mock(DefaultHttpClient.class);
  10. HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
  11. PowerMockito.whenNew(DefaultHttpClient.class).withNoArguments().thenReturn(defaultHttpClientMock);
  12. PowerMockito.when(defaultHttpClientMock.execute(httpPost)).thenReturn(httpResponse);
  13. StatusLine statusLine = PowerMockito.mock(StatusLine.class);
  14. PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
  15. Integer expected = new Integer(0);
  16. PowerMockito.when(statusLine.getStatusCode()).thenReturn(expected);
  17. A a = new A();
  18. Assert.assertEquals(expected, a.callMethod());
  19. }
  20. }

解决方法

这是我的课程正在测试:

  1. public class A {
  2. public Integer callMethod(){
  3. return someMethod();
  4. }
  5. private Integer someMethod(){
  6. //Some Code
  7. HttpPost httpPost = new HttpPost(oAuthMessage.URL);
  8. //Some Code
  9. HttpClient httpClient = new DefaultHttpClient();
  10. HttpResponse httpResponse = httpClient.execute(httpPost); ------1
  11. Integer code = httpResponse.getStatusLine().getStatusCode(); ---2
  12. return code;
  13. }

现在我要模拟第1和2行,并返回模拟HttpResponse和代码。

我已经尝试过但是失败了:

  1. @RunWith(PowerMockRunner.class)
  2. @PowerMockIgnore("javax.crypto.*")
  3. public class TestA {
  4. //Spying some things here & Injecting them
  5. @Test
  6. public void testA() {
  7. DefaultHttpClient defaultHttpClientMock = PowerMockito.mock(DefaultHttpClient.class);
  8. HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class,RETURNS_DEEP_STUBS);
  9. HttpClient httpClient = PowerMockito.mock(HttpClient.class);
  10. //HttpResponse httpResponseMock PowerMockito.mock(HttpResponse.class);
  11. HttpPost httpPost = PowerMockito.mock(HttpPost.class);
  12. PowerMockito.whenNew(DefaultHttpClient.class).withNoArguments().thenReturn(defaultHttpClientMock);
  13. PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost); //Returns null. It never returns httpResponse.
  14. PowerMockito.when(httpResponse.getStatusLine().getStatusCode()).thenReturn(anyInt());
  15. //call the method
  16. }

PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost)始终返回null。我希望它返回的模拟对象HttpResponse。我已经阅读了与此错误相关的其他帖子,但不确定该如何处理。有人可以帮忙吗?

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”