Following the docstring in the source of clojure.test, I issue the following in the repl:
(use 'clojure.test)
(is (thrown? ArithmeticException (/ 1 0)))
And other variations like substituting ArithmeticException with java.lang.ArithmeticException. Yet the exception is never captured by clojure.test but rather bubbles up all the way to the top:
> #error { :cause "Divide by zero" :via [{:type java.lang.ArithmeticException :message "Divide by zero" :at
> [clojure.lang.Numbers divide "Numbers.java" 158]}] :trace
> [[clojure.lang.Numbers divide "Numbers.java" 158]
> [clojure.lang.Numbers divide "Numbers.java" 3808] [api.core$eval1305
> invokeStatic "form-init8750388124499546857.clj" 1]
> [api.core$eval1305 invoke "form-init8750388124499546857.clj" 1]
> [clojure.lang.Compiler eval "Compiler.java" 6927]
> [clojure.lang.Compiler eval "Compiler.java" 6890] [clojure.core$eval
> invokeStatic "core.clj" 3105] [clojure.core$eval invoke "core.clj"
> 3101] [clojure.main$repl$read_eval_print__7408$fn__7411 invoke
> "main.clj" 240] [clojure.main$repl$read_eval_print__7408 invoke
> "main.clj" 240] [clojure.main$repl$fn__7417 invoke "main.clj" 258]
> [clojure.main$repl invokeStatic "main.clj" 258] [clojure.main$repl
> doInvoke "main.clj" 174] [clojure.lang.RestFn invoke "RestFn.java"
> 1523]
> [clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__667
> invoke "interruptible_eval.clj" 87] [clojure.lang.AFn applyToHelper
> "AFn.java" 152] [clojure.lang.AFn applyTo "AFn.java" 144]
> [clojure.core$apply invokeStatic "core.clj" 646]
> [clojure.core$with_bindings_STAR_ invokeStatic "core.clj" 1881]
> [clojure.core$with_bindings_STAR_ doInvoke "core.clj" 1881]
> [clojure.lang.RestFn invoke "RestFn.java" 425]
> [clojure.tools.nrepl.middleware.interruptible_eval$evaluate
> invokeStatic "interruptible_eval.clj" 85]
> [clojure.tools.nrepl.middleware.interruptible_eval$evaluate invoke
> "interruptible_eval.clj" 55]
> [clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__712$fn__715
> invoke "interruptible_eval.clj" 222]
> [clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__707
> invoke "interruptible_eval.clj" 190] [clojure.lang.AFn run
> "AFn.java" 22] [java.util.concurrent.ThreadPoolExecutor runWorker
> "ThreadPoolExecutor.java" 1142]
> [java.util.concurrent.ThreadPoolExecutor$Worker run
> "ThreadPoolExecutor.java" 617] [java.lang.Thread run "Thread.java"
> 745]]}
What might it be? Other stuff from clojure.test does work in the repl as expected.
Version information:
Leiningen 2.7.1 on Java 1.8.0_101 Java HotSpot(TM) 64-Bit Server VM
> nREPL server started on port 44025 on host 127.0.0.1 -
> nrepl://127.0.0.1:44025 REPL-y 0.3.7, nREPL 0.2.12 Clojure 1.8.0 Java
> HotSpot(TM) 64-Bit Server VM 1.8.0_101-b13
use it/wrap it in (clojure.test/deftest...)
assuming:
(require '[clojure.test :as t])
pass example
(t/deftest a
(t/testing "a test"
(t/is (thrown? ArithmeticException (/ 1 0)))))
(a)
nil
fail example
(t/deftest a
(t/testing "a test"
(t/is (thrown? NullPointerException (/ 1 0)))))
(a)
; ERROR in (a) (Numbers.java:158)
; a test
; expected: (thrown? NullPointerException (/ 1 0))
; actual: java.lang.ArithmeticException: Divide by zero
you can also use t/run-tests to get a summary/stats
alternatively it seems you can use t/test-var directly like so
(t/test-var (t/is (thrown? ArithmeticException (/ 1 0))))
nil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With