Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch Exception in Unit Test

How can I set the expectancy of an exception to be thrown in a test in Clojure?

Thanks in advance!

like image 273
Hugo Avatar asked Aug 09 '11 18:08

Hugo


People also ask

How do you handle exceptions in unit testing?

If a method under test declares an exception in its signature, then the test method can allow the exception to propagate to the ABAP Unit runtime. It is best that the test method declares the exception so that the possible behavior of the method is clear. The test method should not catch the exception. .

How do you test catch exception?

You can add exception in test method signature. Then, if you are testing whether exception is thrown, you have to use @Test(expected=Exception. class) . In the test cases where exception has not to be thrown, test will pass successfully.

Can we use try-catch in unit test?

This answer addresses the try/catch part of your question only. If an exception is thrown from the code being tested (or the test code) then the test is a fail and that is normally what you want to happen. and as not all exceptions are catchable (e.g. governor limit ones) it will not even always execute.

Should unit tests cover exceptions?

Exceptions are an integral part of the application. We understood that Unit test cases should be written for validated Technical or Business exceptions. Unit test cases for exceptions should improve the stability and robustness of your application.


1 Answers

(is (thrown? NumberFormatException (Integer/parseInt "49FREN")))
like image 123
amalloy Avatar answered Sep 16 '22 19:09

amalloy