• <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'>

      1. <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>

      2. 如何使用 PowerMockito 模拟构造函数

        How to mock constructor with PowerMockito(如何使用 PowerMockito 模拟构造函数)
      3. <tfoot id='tea1y'></tfoot>
          <tbody id='tea1y'></tbody>
          <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 模拟构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我第一次尝试使用 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模板网!

                  【网站声明】本站部分内容来源于互联网,旨在帮助大家更快的解决问题,如果有图片或者内容侵犯了您的权益,请联系我们删除处理,感谢您的支持!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)

                1. <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>

                  • <small id='KQYmw'></small><noframes id='KQYmw'>

                        <tbody id='KQYmw'></tbody>

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