PowerMockito.doReturn返回null

PowerMockito.doReturn返回null,第1张

PowerMockito.doReturn返回null

代替

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5507465.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存