Skip to content

Instantly share code, notes, and snippets.

@mneedham
Last active December 20, 2015 07:39
Show Gist options
  • Select an option

  • Save mneedham/6095082 to your computer and use it in GitHub Desktop.

Select an option

Save mneedham/6095082 to your computer and use it in GitHub Desktop.
Attempt to stub out google call with WebStub
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static com.thoughtworks.webstub.StubServerFacade.newServer;
import static com.thoughtworks.webstub.dsl.builders.ResponseBuilder.response;
import static org.junit.Assert.assertEquals;
public class RandomTest {
private static com.thoughtworks.webstub.StubServerFacade server;
private static com.thoughtworks.webstub.dsl.HttpDsl stubServer;
@Test
public void jerseyFailingExample() {
Foo foo = new Foo(Client.create());
stubServer.reset();
stubServer.get("http://www.google.com").returns(response(500));
String result = foo.doSomething();
assertEquals("fail", result);
}
@BeforeClass
public static void beforeAll() {
server = newServer(9099);
stubServer = server.withContext("/context");
server.start();
}
@Before
public void setUp() {
stubServer.reset();
}
@AfterClass
public static void afterAll() {
server.stop();
}
class Foo {
private Client httpClient;
public Foo(Client httpClient) {
this.httpClient = httpClient;
}
public String doSomething() {
ClientResponse response = httpClient.resource("http://www.google.com").get(ClientResponse.class);
if(response.getStatus() >= 100 && response.getStatus() < 400) {
return "win";
} else {
return "fail";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment