代替
PowerMockito.doReturn(httpResponse).when(httpClient).execute(httpPost);
你应该使用
PowerMockito.when(httpResponse.execute(httpPost)).thenReturn(httpResponse);
您的测试中还存在一些问题:模拟构造函数不正确,并且根本不需要httpResponse。
更新 此代码对我而言正常工作:
@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()); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)