I'm using mockito in a junit test. How do you make an exception happen and then assert that it has (generic pseudo-code)
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Unrelated to mockito, one can catch the exception and assert its properties. To verify that the exception did happen, assert a false condition within the try block after the statement that throws the exception.
If you want to test the exception message as well you can use JUnit's ExpectedException with Mockito:
Using mockito, you can make the exception happen.
when(testingClassObj.testSomeMethod).thenThrow(new CustomException());
Using Junit5, you can assert exception, asserts whether that exception is thrown when testing method is invoked.
Find a sample here: assert exception junit
To answer your second question first. If you're using JUnit 4, you can annotate your test with
to assert that an exception has occured. And to "mock" an exception with mockito, use
If you're using JUnit 4, and Mockito 1.10.x Annotate your test method with:
and to throw your desired exception use: