2009/9/28 C. Florian Ebeling <florian.ebel...@gmail.com>

>
> > In Java I'd just have an interface
> > with two implementations, and bootstrap the tests with a different
> > implementation, in clojure I guess I'd do something like:
> > (in-ns `some.thing)
> > (defn foo [] nil)
> > or something?
>
> That would consitute a root binding because of the 'defn, but you need
> a dynamic rebinding. This is what you can use:
>
> (binding [existing-function (fn [a b] ...)]
>   ;; your tests here
>    )
>

But as soon as your application becomes smart enough to do things in
parallel (e.g. create and send an e-mail in a separate thread), just using
dynamic binding in tests may not be enough, still the binding will not be
seen by default by other threads.



>
> For mocking java interfaces and classes I'm quite happy with Mockito
> [1], even though it's usage doesn't look always intuitive in clojure.
> This is an example for mock-testing a hadoop map function.
>
> (defn mock [#^Class class]
>  (. Mockito mock class))
>
> (defn mock-output []
>  (mock OutputCollector))
>
> (defn mock-mapper []
>  (mock namespace.surveyed-users-mapper0))
>
> (defn mock-reducer []
>  (mock namespace.surveyed-users-reducer0))
>
> (defn mock-reporter []
>  (mock Reporter))
>
> (defn mock-conf []
>  (mock JobConf))
>
> (defn mock-filesystem []
>  (mock FileSystem))
>
> (defn verify
>  ([mock]
>     (. Mockito verify mock))
>  ([mock mode]
>     (. Mockito verify mock mode)))
>
> (defn stub [mock]
>  (. Mockito when mock))
>
> (deftest mapper0-map-click-test
>  (let [output (mock-output)
>        mapper (mock-mapper)
>        key (to-key nil)
>        value (Text. (tab "9900" "550" "990" "4040" "1" "3"))
>        reporter (mock-reporter)]
>    (doto (stub (.getState mapper))
>      (.thenReturn (ref {:network "9900"
>                         :input-file-name
> "/path/clicks/week/33/result/part-00000"
>                         :pagerules-table {"4040" ["1" "2" "3"]}})))
>    (mapper0-map mapper key value output reporter)
>    (verify output)
>    (.collect output (Text. "550") (Text. "0\t1,2,3"))))
>
> You need to import all the unqualified classes used here, of course.
>
> Florian
>
> [1] http://mockito.org/
>
> --
> Florian Ebeling
> florian.ebel...@gmail.com
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to