How to check that an AssertionException is thrown in a Clojure test?

I am trying to write unit test for my conditions :pre and :post , and my first thought was to do this:

 (ns myproj.battle-test (:use clojure.test myproj.battle)) (deftest lose-only-positive-amount-of-health (let [actor {:health 100}] (is (thrown? AssertionException (:health (lose-health actor -5)))))) 

But I can't figure out how to refer to an AssertionException from my test file and get an exception instead:

 java.lang.IllegalArgumentException: Unable to resolve classname: AssertionException 

I tried different things (and Google was not useful, apparently, because it is too simple a question), but with no luck, so how can I check that an AssertionException was thrown?

I understand that this is a very important issue; these are my first few lines of Clojure :)

+4
source share
1 answer

Exceptions are classes, so just import them.

 (ns myproj.battle-test (:use clojure.test myproj.battle) (:import a.package.which.contains.AssertionException)) ... 

Find where (in which package) the AssertionException is located, and replace it with a.package.which.contains in the above code.

Or you can use the full name instead, as in the sentence :import above, but it can be tedious if you have several places where you use the class.

UPD I made a mistake. There is no such class, AssertionException . There is an AssertionError class, and since it is inside the java.lang , it is automatically imported. Clojure pre / post conditions throw it, so use an AssertionError instead of an AssertionException , and your code should work fine.

+4
source

All Articles