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

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

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

        异常Junit 4.10后的Mockito验证

        时间:2023-09-26
        <legend id='POc35'><style id='POc35'><dir id='POc35'><q id='POc35'></q></dir></style></legend>
          <bdo id='POc35'></bdo><ul id='POc35'></ul>
        • <tfoot id='POc35'></tfoot>

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

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

                  本文介绍了异常Junit 4.10后的Mockito验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在测试一个带有预期异常的方法.我还需要验证在抛出异常后(在模拟对象上)调用了一些清理代码,但看起来验证被忽略了.这是代码.我正在使用 Junit ExpectedException Rule 来验证预期的异常.

                  I am testing a method with an expected exception. I also need to verify that some cleanup code was called (on a mocked object) after the exception is thrown, but it looks like that verification is being ignored. Here is the code. I am using the Junit ExpectedException Rule to verify the expected exception.

                  @Rule
                  public ExpectedException expectedEx = ExpectedException.none();
                  
                  @Test
                  public void testExpectedException()
                  {
                     MockedObject mockObj = mock(MockedObj.class);
                     MySubject subject = new MySubject(mockedObj);
                     expectedEx.expect(MyException.class);
                     expectedEx.expectMessage("My exception message.");
                     subject.someMethodThrowingException();
                     verify(mockObj).
                         someCleanup(eq(...));
                  }
                  

                  似乎 verify 完全被忽略了.无论我在 verify 中输入什么方法,我的测试都通过了,这不是我想要的.

                  It seems like the verify is totally being ignored. No matter what method I put in the verify, my test is passing, which is not what I want.

                  知道为什么会这样吗?

                  推荐答案

                  ExpectedException 由 通过 github.com/junit-team/junit/blob/master/src/main/java/org/junit/rules/TestRule.java" rel="noreferrer">JUnit @Rule.当您的代码抛出异常时,它会向上堆栈到最近的 try/catch,这恰好位于 ExpectedException 实例中(它会检查它是否是您期望的异常).

                  ExpectedException works by wrapping your entire test method in a try-catch block via a JUnit @Rule. When your code throws an exception, it goes up the stack to the nearest try/catch, which happens to be in the ExpectedException instance (which checks that it is the exception you're expecting).

                  在 Java 中,如果方法中发生未捕获的异常,则控制权将永远不会返回到该方法中稍后的语句.此处适用相同的规则:在异常发生后,控制永远不会返回到测试中的语句.

                  In Java, if an uncaught exception occurs in a method, control will never return to statements later in that method. The same rules apply here: Control never returns to the statements in your test after the exception.

                  从技术上讲,您可以将验证放在 finally 块中,但这往往是 一个坏习惯.您的被测系统可能会抛出意外异常,或者根本没有异常,这将为您提供有用的失败消息和跟踪;但是,如果该失败导致您的验证或断言在 finally 块中失败,那么 Java 将显示这一点,而不是有关意外异常或意外成功的消息.这会使调试变得困难,尤其是因为您的错误将来自错误根本原因之后的代码行,错误地暗示上面的代码已成功.

                  Technically, you could put the verifications in a finally block, but that tends to be a bad habit. Your system-under-test might throw an unexpected exception, or no exception at all, which would give you a helpful failure message and trace; however, if that failure then causes your verifications or assertions to fail in the finally block, then Java will show that rather than a message about the unexpected exception or unexpected success. This can make debugging difficult, especially because your error will come from lines of code following the root cause of the error, incorrectly implying that the code above it succeeded.

                  如果您确实需要在异常发生后验证状态,则可以基于每个方法,随时恢复到这个习惯用法:

                  If you really need to verify state after the exception, on a per-method basis, you can always revert back to this idiom:

                  @Test
                  public void testExpectedException()
                  {
                    MockedObject mockObj = mock(MockedObj.class);
                    MySubject subject = new MySubject(mockedObj);
                    try {
                      subject.someMethodThrowingException();
                      fail("Expected MyException.");
                    } catch (MyException expected) {
                      assertEquals("My exception message.", expected.getMessage());
                    }
                    verify(mockObj).someCleanup(eq(...));
                  }
                  

                  更新:使用 Java 8 的 lambda 表达式,您可以将函数式接口调用包装在 try 块中 简洁到有用.我想对这种语法的支持会在许多标准测试库中找到.

                  Update: With Java 8's lambda expressions, you can wrap a functional interface call in a try block concisely enough to be useful. I imagine support for this syntax will find its way into many standard testing libraries.

                  assertThrows(MyException.class,
                      () -> systemUnderTest.throwingMethod());
                  

                  这篇关于异常Junit 4.10后的Mockito验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 PowerMockito 1.6 验证静态方法调用 下一篇:Mockito 验证特定的 lambda 已作为参数传递给 mock 的

                  相关文章

                  最新文章

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

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

                    <bdo id='VHhyw'></bdo><ul id='VHhyw'></ul>
                1. <tfoot id='VHhyw'></tfoot>

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