• <tfoot id='tea1y'></tfoot>
    • <bdo id='tea1y'></bdo><ul id='tea1y'></ul>

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

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

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

        如何使用 PowerMockito 模拟构造函数

        时间:2023-09-26
      1. <legend id='0WjWz'><style id='0WjWz'><dir id='0WjWz'><q id='0WjWz'></q></dir></style></legend>
          <tfoot id='0WjWz'></tfoot>
            <bdo id='0WjWz'></bdo><ul id='0WjWz'></ul>

            <small id='0WjWz'></small><noframes id='0WjWz'>

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

                1. 本文介绍了如何使用 PowerMockito 模拟构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我第一次尝试使用 PowerMockito 模拟类构造函数,但它不起作用.我当前的代码是:

                  I'm trying to mock a class constructor with PowerMockito for first time, but it doesn't work. My current code is:

                  public class Bar {
                      public String getText() {
                          return "Fail";
                      }
                  }
                  
                  public class Foo {
                      public String getValue(){
                          Bar bar= new Bar();
                          return bar.getText();
                      }
                  
                  }
                  
                  @RunWith(PowerMockRunner.class)
                  @PrepareForTest(Bar.class)
                  public class FooTest {
                      private Foo foo;
                      @Mock
                      private Bar mockBar;
                  
                      @Before
                      public void setUp() throws Exception {
                          MockitoAnnotations.initMocks(this);
                          PowerMockito.whenNew(Bar.class).withNoArguments().thenReturn(mockBar);
                          foo= new Foo();
                      }
                  
                      @Test
                      public void testGetValue() throws Exception {
                          when(mockBar.getText()).thenReturn("Success");
                          assertEquals("Success",foo.getValue());
                  
                      }
                  }
                  

                  测试失败,因为返回值为Fail".我的问题在哪里?

                  The test fails because the returned value is "Fail". Where is my problem?

                  推荐答案

                  好的,找到答案了,需要调用

                  Okey, found the answer, you need to call to

                  @PrepareForTest(Foo.class)
                  

                  而不是

                  @PrepareForTest(Bar.class)
                  

                  这篇关于如何使用 PowerMockito 模拟构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:模拟受保护的方法 下一篇:Mockito 可以在方法调用时根据参数的值来验证参数

                  相关文章

                  最新文章

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

                3. <small id='KQYmw'></small><noframes id='KQYmw'>

                        <bdo id='KQYmw'></bdo><ul id='KQYmw'></ul>
                    1. <legend id='KQYmw'><style id='KQYmw'><dir id='KQYmw'><q id='KQYmw'></q></dir></style></legend>