Skip to content
Advertisement

Test if an exception is caught with Junit

I’ll begin with a code example; I have to test a function, which handles data-packets. In this function, the data-packet is opened and when it doesn’t contain all expected data, an InvalidParameterExeption is thrown which is logged.

JavaScript

So, if everything goes well, my exception is printed in my terminal. But how can I test this? I can’t use this: (because the exception is caught)

JavaScript

How can I test that the InvalidParameterExeption is thrown?

Advertisement

Answer

You won’t catch exceptions that are not thrown. Just test the ‘throwing exception’ method instead of the ‘exception catching’ one

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