PowerMockito.doReturn返回null

编程入门 行业动态 更新时间:2024-10-26 14:35:49
本文介绍了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行. 2&返回模拟HttpResponse&代码.

Now I want to mock the line 1 & 2 & return a mock HttpResponse & code.

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

I have tried this but failed:

@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

本文发布于:2023-10-31 07:13:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PowerMockito   doReturn   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!