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

5

52

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

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

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?


}
Boutwell answered 10/8, 2016 at 8:56 Comment(3)
You don't need to do this with Mockito: JUnit will fail your test if an exception is thrown when that method is called. So... you don't need to do anything extra.Peebles
ok but it doesn't seem like its actually testing anything? i.e. there are no verifys or asserts etc?Boutwell
Currently, you are simply testing that this method doesn't throw an exception. You are free to add more checks, e.g. that getBalanceForPerson returns the expected value - but that's a JUnit check, assertEquals(expectedBalance, result).Peebles
R
65

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");
   }
}
Rosiarosicrucian answered 10/8, 2016 at 9:13 Comment(1)
this is the old way of catching exceptions. Answer below should be the answer for doing it with JUnit 5.xBickart
M
40

If you are using Mockito 5.2 or later then you're able to use assertDoesNotThrow

Assertions.assertDoesNotThrow(() -> myClass.getBalanceForPerson(person1););
Microelectronics answered 3/7, 2020 at 11:10 Comment(4)
flawless answer!Ingratiate
AssertJ also has a similar method: assertThatCode(() -> ...).doesNotThrowAnyException();Classic
What if we need to check if a particular exception is not thrown?Millen
Just note this has nothing to do with Mockito. Assertions.assertDoesNotThrow is from JUnit 5.Rhizocarpous
S
10

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

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.

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.

Stemma answered 10/8, 2016 at 9:27 Comment(0)
V
2

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

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
    }
}
Vulnerable answered 13/9, 2018 at 5:36 Comment(0)
D
0

If like me you use AssertJ, it will be

assertThatCode(() -> {
  // code that should throw an exception
  ...
}).doesNotThrowAnyException();

https://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#:~:text=You%20can%20test%20that%20a%20piece%20of%20code%20does%20not%20throw%20any%20exception%3A

Donnell answered 15/4 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.