Skip to content
Advertisement

Mockito How to mock and assert a thrown exception

I have this junit:

JavaScript

but I have this error:

JavaScript

I also tried:

JavaScript

Advertisement

Answer

The problem is that you are stubbing, but not really testing anything. And if you are not testing anything, then there is no need for stubbing. That’s why you have unnecessary Mockito stubbings.

I assume that you want to test your SecurityManagerService. If you want to do that, you need to create an instance or have a bean of that type, but not a mock. Then you call the method that you want to test and assert that you get the expected result.

If you expect to get an exception you can test it like this:

JUnit4, just enhance you @Test annotation:

JavaScript

JUnit 5:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement