I am having a hard time creating a simple unit test in Clojure using with-redefs
for mocking on Windows. The test code, the function I'm about to test and the function to be mocked are all in different namespaces/files:
Function under test:
(ns com.mynamespace.a
(:require [com.mynamespace.b :as b] ) )
(defn afunc [param]
(b/bfunc param))
Dependent b namespace to be mocked out:
(ns com.mynamespace.b)
(defn bfunc [param]
; External call
)
Test code
(ns com.mynamespace.a-test
(:require [com.mynamespace.a :as a]
[com.mynamespace.b :as b] )
(:use [clojure.test]))
(deftest a-tests
(with-redefs [b/bfunc (constantly "dummy")]
(print (a/afunc "test"))
)
)
I think I use with-redefs
correctly, because my unit test runs fine on Linux machines (docker container or virtual Ubuntu), but it just does not work when executing/developing them on my Windows dev machine: the with-redefs
bindings are not applied and the test wants to make real e.g. http calls, which I'd like to mock. It is the same case if I run lein test
, or try executing the test from a REPL through Eclipse CCW.
I know with-redefs has some strange behaviour, but as my test works well on Linux, I guess I miss something, when running this project's test on Windows. I would like to achieve fast feedback loops, when writing tests on my Windows machine.
Do you have any idea what I'm doing wrong? Thanks, Andras
Turned out that this issue was specific to a project I'm working on, because of the -Dclojure.compiler.direct-linking=true jvm-opts flag.
After turning off direct linking, my tests work on my Windows dev machine as well. I wonder why they passed on Linux though, but perhaps it was not applied, when running lein test
there.