Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?


}
like image 813
java123999 Avatar asked Aug 10 '16 08:08

java123999


People also ask

How do you test if an exception is not thrown?

If you want to test a scenario in which an exception should be thrown then you should use the expected annotation. If you want to test a scenario where your code fails and you want to see if the error is correctly handled: use expected and perhaps use asserts to determine if it's been resolved.

How do you check if an exception is thrown JUnit?

When using JUnit 4, we can simply use the expected attribute of the @Test annotation to declare that we expect an exception to be thrown anywhere in the annotated test method. In this example, we've declared that we're expecting our test code to result in a NullPointerException.


4 Answers

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");
   }
}
like image 152
UserF40 Avatar answered Oct 24 '22 14:10

UserF40


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

Assertions.assertDoesNotThrow(() -> myClass.getBalanceForPerson(person1););
like image 44
user634545 Avatar answered Oct 24 '22 14:10

user634545


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.

like image 10
125_m_125 Avatar answered Oct 24 '22 14:10

125_m_125


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
    }
}
like image 2
MLS Avatar answered Oct 24 '22 12:10

MLS