Last active
December 20, 2015 07:39
-
-
Save mneedham/6095082 to your computer and use it in GitHub Desktop.
Attempt to stub out google call with WebStub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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