I am writing a Java class that uses Jersey under the hood to send an HTTP request to a RESTful API (third party).
I would also like to write a JUnit test that makes fun of the API sending HTTP 500 responses. As a newcomer to Jersey, I have a hard time understanding what I need to do to mock these HTTP 500 responses.
So far this is my best attempt:
// The main class-under-test public class MyJerseyAdaptor { public void send() { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); String uri = UriBuilder.fromUri("http://example.com/whatever").build(); WebResource service = client.resource(uri); // I *believe* this is where Jersey actually makes the API call... service.path("rest").path("somePath") .accept(MediaType.TEXT_HTML).get(String.class); } } @Test public void sendThrowsOnHttp500() { // GIVEN MyJerseyAdaptor adaptor = new MyJerseyAdaptor(); // WHEN try { adaptor.send(); // THEN - we should never get here since we have mocked the server to // return an HTTP 500 org.junit.Assert.fail(); } catch(RuntimeException rte) { ; } }
I am familiar with Mockito, but have no preference in the mocking library. Basically, if someone can just tell me which classes / methods need to be mocked to throw an HTTP 500 response, I can figure out how to actually implement the layouts.
source share