• <bdo id='fvyaf'></bdo><ul id='fvyaf'></ul>

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

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

    3. Mockito 绕过静态方法进行测试

      时间:2023-09-25
      <i id='m0hxV'><tr id='m0hxV'><dt id='m0hxV'><q id='m0hxV'><span id='m0hxV'><b id='m0hxV'><form id='m0hxV'><ins id='m0hxV'></ins><ul id='m0hxV'></ul><sub id='m0hxV'></sub></form><legend id='m0hxV'></legend><bdo id='m0hxV'><pre id='m0hxV'><center id='m0hxV'></center></pre></bdo></b><th id='m0hxV'></th></span></q></dt></tr></i><div id='m0hxV'><tfoot id='m0hxV'></tfoot><dl id='m0hxV'><fieldset id='m0hxV'></fieldset></dl></div>

    4. <small id='m0hxV'></small><noframes id='m0hxV'>

        • <bdo id='m0hxV'></bdo><ul id='m0hxV'></ul>
          • <legend id='m0hxV'><style id='m0hxV'><dir id='m0hxV'><q id='m0hxV'></q></dir></style></legend>
              <tbody id='m0hxV'></tbody>
            <tfoot id='m0hxV'></tfoot>

                本文介绍了Mockito 绕过静态方法进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我需要使用 Mockito 测试 handleIn() 方法.

                I need to test handleIn() method using Mockito.

                但是代码需要调用这个遗留代码 Util.getContextPDO 这是一个静态方法.

                However the code need to call this legacy code Util.getContextPDO which is a static method.

                请注意,在测试环境中,这个 Util.getContextPDO 总是返回异常,我打算通过总是返回一个虚拟 IPDO 来绕过这个 Util.getContextPDO().

                Note that in testing environment this Util.getContextPDO is always returns Exception, and I intend to bypass this Util.getContextPDO() by always return a dummy IPDO.

                public class MyClass {
                  public IPDO getIPDO() 
                  {
                    return Util.getContextPDO(); // note that Util.getContextPDO() is a static, not mockable.
                  }
                
                  public String handleIn(Object input) throws Throwable 
                  {
                    String result = "";
                    IPDO pdo = getIPDO();
                
                    // some important business logic.
                
                    return result;
                  } 
                }
                

                最初我认为这可以通过使用MyClass"类的 spy() 来实现,因此我可以模拟 getIPDO() 的返回值.下面是我使用 spy() 的初步尝试

                Initially I thought this achieveable by using spy() of the class "MyClass", so I can mock the return value of getIPDO(). Below is my initial effort using spy ()

                @Test
                public void testHandleIn() throws Exception
                {
                    IPDO pdo = new PDODummy();
                
                
                    MyClass handler = new MyClass ();
                    MyClass handler2 = spy(handler);
                
                    when(handler2.getIPDO()).thenReturn(pdo);
                    PDOUtil.setPDO(pdo, LogicalFieldEnum.P_TX_CTGY, "test123");
                    IPDO pdoNew = handler2.getIPDO();
                
                    Assert.assertEquals("test123,(PDOUtil.getValueAsString(pdoNew, LogicalFieldEnum.P_TX_CTGY)));
                
                }
                

                但是 when(handler2.getIPDO()).thenReturn(pdo); 正在抛出我想避免的异常(因为 handler2.getIPDO() )似乎调用了真正的方法.

                However the when(handler2.getIPDO()).thenReturn(pdo); is throwing the Exception that I want to avoid ( because handler2.getIPDO() ) seems to call the real method.

                知道如何测试这部分代码吗?

                Any idea on how to test this part of code?

                推荐答案

                将我的测试改为:

                @Test
                public void testHandleIn() throws Exception
                {
                  IPDO pdo = new PDODummy();
                
                
                  MyClass handler = new MyClass ();
                  MyClass handler2 = spy(handler);
                
                  doReturn(pdo ).when( handler2 ).getIPDO();
                  PDOUtil.setPDO(pdo, LogicalFieldEnum.P_TX_CTGY, "test123");
                  IPDO pdoNew = handler2.getIPDO();
                
                  Assert.assertEquals("test123,(PDOUtil.getValueAsString(pdoNew, LogicalFieldEnum.P_TX_CTGY)));
                
                }
                

                阅读Effective Mockito后解决.

                这篇关于Mockito 绕过静态方法进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Mockito 模拟所有方法调用并返回 下一篇:如何在 tycho 中引用 mockito?

                相关文章

                最新文章

                • <bdo id='IwKbS'></bdo><ul id='IwKbS'></ul>

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

                  <tfoot id='IwKbS'></tfoot>

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

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