5

This works for me :

(is  (thrown? AbstractMethodError (.fun obj 1) ))

But this blows up

(is (not (thrown? AbstractMethodError (.fun obj 1) )))

with the following error :

java.lang.RuntimeException: Unable to resolve symbol: thrown? in this context

Presumably "thrown?" has some special meaning inside the "is" and isn't visible when I put the "not" in the way.

So how can I test a negation of an assertion like thrown?

interstar
  • 26,048
  • 36
  • 112
  • 180
  • possible duplicate of [Clojure unit-testing. How do I test if a function throws an exception?](http://stackoverflow.com/questions/32063244/clojure-unit-testing-how-do-i-test-if-a-function-throws-an-exception) – Nathan Davis Aug 18 '15 at 16:12
  • @NathanDavis It's related but this is meant to be a distinct part of a larger question. – interstar Aug 18 '15 at 16:41

1 Answers1

5

You're correct, thrown? is a special assertion that must appear after is. If you want to test that an exception is not thrown (in other words, the call evaluated to something), you might want to do something like:

(is (.fun obj 1))

although that has one problem: it would fail if the result is falsey (false, nil). To be completely sure:

(let [r (.fun obj 1)]
  (is (= r r)))

(this is a tautology, and will only fail if it does throw an exception).

Diego Basch
  • 12,764
  • 2
  • 29
  • 24
  • 1
    Actually, what I'm really interested in testing is whether obj actually HAS a method .fun. Can you think of a better way to do this? – interstar Aug 18 '15 at 04:55
  • 1
    Via the reflection api. See this answer, for example: http://stackoverflow.com/questions/5821286/how-can-i-get-the-methods-of-a-java-class-from-clojure – Diego Basch Aug 18 '15 at 05:18