I see there's a way to test if a function throws an exception of class C. But is there a way to test whether a function throws any exception. Or to assert that it should NOT throw an exception?
Asked
Active
Viewed 7,160 times
10
-
possible duplicate of [Clojure testing : testing the negation of an assertion](http://stackoverflow.com/questions/32063505/clojure-testing-testing-the-negation-of-an-assertion) – Nathan Davis Aug 18 '15 at 16:11
1 Answers
17
For tests that don't expect exceptions, write your test as normal. Any exceptions thrown will fail the test.
For tests that could throw any exception, then use Exception
or Throwable
(Exception's superclass).
For example:
(deftest mytest
(is (thrown? Exception (/ 1 0))))
(/ 1 0)
will throw a java.lang.ArithmeticException
but will also be matched by it's parent class java.lang.Exception
.
You could also write a not-thrown?
macro to do the opposite of the thrown?
macro in clojure.test.
As a side note, you generally want to catch more specific errors when you're unit testing, as your code may throw a new unexpected error but your tests will happily pass.

Daniel Compton
- 13,878
- 4
- 40
- 60
-
1Actually, what I really want to do is assert that my function does NOT raise any Exception. See this http://stackoverflow.com/questions/32063505/clojure-testing-testing-the-negation-of-an-assertion That's why I don't care much about the kind of exception. – interstar Aug 18 '15 at 04:26
-
If you only want to assert that an exception isn't thrown then just write a normal test to check the result of the call you made. If there is no useful information you can get from the result of your function call, and you just expect it to succeed, then you could put `(= 1 1)` for your test, and any exceptions thrown should bubble up and fail the test. – Daniel Compton Aug 18 '15 at 04:43
-
Looking at your other question, you should test the result of `(.fun obj 1)`. If an exception is thrown then it will fail the test. – Daniel Compton Aug 18 '15 at 04:45
-
What I'm really trying to do is to test if a method exists. I'm doing this lots of times, in order to check whether various record types that are meant to implement a protocol actually do. (Since the compiler won't do this for me. http://stackoverflow.com/questions/32027613/is-there-a-clojure-compile-time-tool-to-check-if-a-record-or-type-actually-imple/32030000) I don't really want to have to think about the specific VALUES that all the different possible classes might produce for a call of .fun. Just "does this blow up because .fun isn't there?" – interstar Aug 18 '15 at 04:53
-
2You could write your own `not-thrown?` macro to do the opposite of this: https://github.com/clojure/clojure/blob/master/src/clj/clojure/test.clj#L490-L502 – Daniel Compton Aug 18 '15 at 05:15
-
Yes, I wrote the not-thrown? macro which works for me. If you make that part of the answer I'll accept it. – interstar Aug 19 '15 at 19:23
-
1[Commit-tied link](https://github.com/clojure/clojure/blob/8e7213781a1a45db966898835b8f41f545c64d52/src/clj/clojure/test.clj#L504-L516) – Ven Apr 09 '19 at 16:16