Groovy web console

subscribe to the feed Subscribe
to this
site
mock (via #groovywebconsole)
tweet this snippet Tweet
this
script

mock

Published 2 years ago by raphael
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
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 :)"]
    }
}

//usage

void setUp(){
    def mock = new MockHTTPBuilder()
    yourService.httpClient = mock
}

void testSomethingBad(){
    yourService.httpClient.pleaseFail = true
    assertFail yourService.someMethodThatUsesTheHttpClient()
}

void testSomethingGood(){
    yourService.httpClient.pleaseFail = false
    assertTrue yourService.method()
}