marick/Midje — Issue #55: Mocking java method calls
Author: jevgeni
Created: 2011-10-19 07:57:55+00:00
State: open

I am trying to mock java calls using meta constants like this:

```
(ns midje.test.core
(:use [midje.sweet]))

(defn write [socket message]
(.write (.getOutputStream socket) (.getBytes message)))

(fact
(write ...sock.. "hello") => nil
(provided
(.getOutputStream ..sock..) => System/out))
```

I expect that (.getOutputStream ..sock..) will be replaced with System/
out in the write function call. However, I get "java.lang.Exception:
Unable to resolve var: .getOutputStream in this context (core.clj:
10)"

Considering you know which functions are to be mocked:

```
(fact ...
(provided
(.toUpperCase ..meta..) => "LO"))
```
it should be possible to perform something like this during macro evaluation:

```
(gen-interface
:name midje.Stub
:methods [[toUpperCase [] String]])

(def stub
(proxy [midje.Stub] []
(toUpperCase [] "LO")))
```
(assuming that interface name is generated and there is some mechanism
to clean \*compile-path\*)

Then associate ..meta.. with this stub and use it like this:

```
user=> (.toUpperCase stub)
"LO"
```