4

This question is closely related to this question. The difference is that I'd like to follow the recommended approach of mocking the client. So, I have the following HTTPBuilder defined:

protected readUrl() {
    def http = new HTTPBuilder("http://example.com")
    def status = http.request(Method.GET, ContentType.JSON) {req ->
        response.success = {resp, json ->
            result = json.toString()
            new Success<String>(result)
        }
        response.'401' = {resp ->
            final String errMsg = "Not Authorized"
            new Failed(Failable.Fail.ACCESS_DENIED, errMsg)
        }
        response.failure = {resp ->
            final String errMsg = "General failure ${resp.statusLine}"
            new Failed(Failable.Fail.UNKNOWN, errMsg)
        }
    }
}

What I'd like to do is find a way to unit test this code block. I wanted to mock the response so I could specifically set the response codes, if possible. Could someone please show me a way to do this?

Community
  • 1
  • 1
Michael D Johnson
  • 889
  • 1
  • 10
  • 21
  • I have actually coded this by mocking HTTPBuilder, but I'd still like to see what mocking the client looks like. – Michael D Johnson Sep 21 '12 at 22:01
  • what do you mean by mocking "the client" ? – Michael Easter Sep 22 '12 at 17:13
  • 1
    Something like this: http://groovyconsole.appspot.com/script/760001 Simply set this mock to your service. – Raphael Sep 23 '12 at 20:42
  • @Michael I'm not sure what "the client" should be. It was mentioned that mocking "the client" was a better approach in the question that I linked to, however, I didn't know what the poster meant either. – Michael D Johnson Sep 25 '12 at 14:16
  • @Raphael Thank you. I mocked my solution differently, but your code sample looks much simpler than my solution turned out to be. The biggest issue I had was being able to test the paths for different response codes. Your sample seems to have solved that issue. – Michael D Johnson Sep 25 '12 at 14:17
  • Can you post as answer an accept it? So other will not spend time seeing this closed topic. –  Sep 25 '12 at 20:13
  • @Raphael If you will post your comment into an answer I will accept it. – Michael D Johnson Sep 25 '12 at 20:52

1 Answers1

1

This is my prefered solution:

class MockHTTPBuilder{
    MockHTTPBuilder(string){}
    MockHTTPBuilder(){}
    def pleaseFail = false
    def mockData = []
    def request(a, b, c){
        if(pleaseFail) [status:'500',data: mockData ?: "It failed :("]
        else [status:'200',data: mockData ?: "Yay :)"]
    }
}

Here's some sample usages: http://groovyconsole.appspot.com/script/760001

Alternatively, you can metaprogram the actual httpClient instance and make it behave as expected(forcebly failing or passing during test-time) but that's a little bit more complicated.

Raphael
  • 1,760
  • 1
  • 12
  • 21