<bdo id='tHIIK'></bdo><ul id='tHIIK'></ul>
    1. <tfoot id='tHIIK'></tfoot>
      <legend id='tHIIK'><style id='tHIIK'><dir id='tHIIK'><q id='tHIIK'></q></dir></style></legend>
      <i id='tHIIK'><tr id='tHIIK'><dt id='tHIIK'><q id='tHIIK'><span id='tHIIK'><b id='tHIIK'><form id='tHIIK'><ins id='tHIIK'></ins><ul id='tHIIK'></ul><sub id='tHIIK'></sub></form><legend id='tHIIK'></legend><bdo id='tHIIK'><pre id='tHIIK'><center id='tHIIK'></center></pre></bdo></b><th id='tHIIK'></th></span></q></dt></tr></i><div id='tHIIK'><tfoot id='tHIIK'></tfoot><dl id='tHIIK'><fieldset id='tHIIK'></fieldset></dl></div>
    2. <small id='tHIIK'></small><noframes id='tHIIK'>

        PowerMockito.doReturn 返回 null

        时间:2023-09-26
            <tbody id='JOIlJ'></tbody>
            <bdo id='JOIlJ'></bdo><ul id='JOIlJ'></ul>
            • <tfoot id='JOIlJ'></tfoot>

              <small id='JOIlJ'></small><noframes id='JOIlJ'>

              <i id='JOIlJ'><tr id='JOIlJ'><dt id='JOIlJ'><q id='JOIlJ'><span id='JOIlJ'><b id='JOIlJ'><form id='JOIlJ'><ins id='JOIlJ'></ins><ul id='JOIlJ'></ul><sub id='JOIlJ'></sub></form><legend id='JOIlJ'></legend><bdo id='JOIlJ'><pre id='JOIlJ'><center id='JOIlJ'></center></pre></bdo></b><th id='JOIlJ'></th></span></q></dt></tr></i><div id='JOIlJ'><tfoot id='JOIlJ'></tfoot><dl id='JOIlJ'><fieldset id='JOIlJ'></fieldset></dl></div>

                  <legend id='JOIlJ'><style id='JOIlJ'><dir id='JOIlJ'><q id='JOIlJ'></q></dir></style></legend>
                  本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:通缉但不调用:Mockito PrintWriter 下一篇:如何使用 Mockito 验证重载方法的调用次数?

                  相关文章

                  最新文章

                  1. <i id='QZ0yb'><tr id='QZ0yb'><dt id='QZ0yb'><q id='QZ0yb'><span id='QZ0yb'><b id='QZ0yb'><form id='QZ0yb'><ins id='QZ0yb'></ins><ul id='QZ0yb'></ul><sub id='QZ0yb'></sub></form><legend id='QZ0yb'></legend><bdo id='QZ0yb'><pre id='QZ0yb'><center id='QZ0yb'></center></pre></bdo></b><th id='QZ0yb'></th></span></q></dt></tr></i><div id='QZ0yb'><tfoot id='QZ0yb'></tfoot><dl id='QZ0yb'><fieldset id='QZ0yb'></fieldset></dl></div>
                        <bdo id='QZ0yb'></bdo><ul id='QZ0yb'></ul>

                      <legend id='QZ0yb'><style id='QZ0yb'><dir id='QZ0yb'><q id='QZ0yb'></q></dir></style></legend>

                      <small id='QZ0yb'></small><noframes id='QZ0yb'>

                    1. <tfoot id='QZ0yb'></tfoot>