Java 如何使用mockito检查未抛出异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38868620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:42:07  来源:igfitidea点击:

How to check that an exception is not thrown using mockito?

javaunit-testingexceptionexception-handlingmockito

提问by java123999

I have a simple Javamethod, I would like to check that it does not throw any exceptions.

我有一个简单的Java方法,我想检查它是否不会抛出任何exceptions.

I have already mocked the parameters etc, however I am not sure how to use Mockitoto test that no exception has been thrown from the method?

我已经模拟了参数等,但是我不确定如何使用Mockito来测试该方法没有抛出异常?

Current test code:

当前测试代码:

  @Test
  public void testGetBalanceForPerson() {

   //creating mock person
   Person person1 = mock(Person.class);
   when(person1.getId()).thenReturn("mockedId");

  //calling method under test
  myClass.getBalanceForPerson(person1);

  //How to check that an exception isn't thrown?


}

采纳答案by UserF40

Fail the test if an exception is caught.

如果捕获到异常,则测试失败。

@Test
public void testGetBalanceForPerson() {

   // creating mock person
   Person person1 = mock(Person.class);
   when(person1.getId()).thenReturn("mockedId");

   // calling method under test
   try {
      myClass.getBalanceForPerson(person1);
   } catch(Exception e) {
      fail("Should not have thrown any exception");
   }
}

回答by 125_m_125

As long as you are not explicitly stating, that you are expecting an exception, JUnit will automatically fail any Tests that threw uncaught Exceptions.

只要您没有明确声明您期待异常,JUnit 就会自动使任何抛出未捕获异常的测试失败。

For example the following test will fail:

例如,以下测试将失败:

@Test
public void exampleTest(){
    throw new RuntimeException();
}

If you further want to check, that the test will fail on Exception, you could simply add a throw new RuntimeException();into the method you want to test, run your tests and check if they failed.

如果您想进一步检查测试是否会因异常而失败,您可以简单地将 a 添加throw new RuntimeException();到您要测试的方法中,运行您的测试并检查它们是否失败。

When you are not manually catching the exception and failing the test, JUnit will include the full stack trace in the failure message, which allows you to quickly find the source of the exception.

当您不手动捕获异常并导致测试失败时,JUnit 将在失败消息中包含完整的堆栈跟踪,这使您可以快速找到异常的来源。

回答by MLS

Using Assertions.assertThatThrownBy().isInstanceOf() twice as shown below would serve the purpose!

如下所示使用 Assertions.assertThatThrownBy().isInstanceOf() 两次即可达到目的!

import org.assertj.core.api.Assertions;
import org.junit.Test;

public class AssertionExample {

    @Test
    public void testNoException(){
        assertNoException();
    }



    private void assertException(){
        Assertions.assertThatThrownBy(this::doNotThrowException).isInstanceOf(Exception.class);
    }

    private void assertNoException(){
        Assertions.assertThatThrownBy(() -> assertException()).isInstanceOf(AssertionError.class);
    }

    private void doNotThrowException(){
        //This method will never throw exception
    }
}