I am trying to write a unit test for my :pre and :post conditions, 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 work out how to reference AssertionException from my test file, and get an exception instead:
java.lang.IllegalArgumentException: Unable to resolve classname: AssertionException
I’ve tried various things (and Google hasn’t been helpful, presumably because this is far too simple a question) but without any luck, so how do I check that AssertionException was thrown?
I realise this is very much a beginner’s question; these are my first few lines of Clojure 🙂
Exceptions are classes, so just import it.
Locate where (in which package)
AssertionExceptionis located and substitute it instead ofa.package.which.containsin the code above.Or you can use full name instead, just like in
:importclause above, but this can be tedious if you have more than one places where you use the class.UPD. I made a mistake. There is no such class,
AssertionException. There is a classAssertionError, and since it is located insidejava.langpackage, it is imported automatically. Clojure pre/post conditions throw it, so just useAssertionErrorinstead ofAssertionException, and your code should run fine.