Skip to content

Instantly share code, notes, and snippets.

@dansiviter
Created April 9, 2018 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansiviter/eae408dbd314b65a59815ad788ea8cfa to your computer and use it in GitHub Desktop.
Save dansiviter/eae408dbd314b65a59815ad788ea8cfa to your computer and use it in GitHub Desktop.
package acme;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.junit.Assert.assertEquals;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.junit.Before;
import org.junit.Test;
public class RestEasyMockTest {
private Dispatcher dispatcher;
@Before
public void before() {
this.dispatcher = MockDispatcherFactory.createDispatcher();
}
@Test
public void get() throws URISyntaxException, UnsupportedEncodingException {
this.dispatcher.getRegistry().addSingletonResource(new MyResource1());
this.dispatcher.getRegistry().addSingletonResource(new MyResource2("Different arg!"));
MockHttpRequest request = MockHttpRequest.get("/path1");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(SC_OK, response.getStatus());
assertEquals("Different arg!", response.getContentAsString()); // <<< fails!
}
// --- Inner Classes ---
@Path("/path1")
public static class MyResource1 {
@Context
public ResourceContext resourceCtx;
@GET
public String get() {
return resourceCtx.getResource(MyResource2.class).get();
}
}
@Path("path2")
public static class MyResource2 {
private final String response;
public MyResource2() {
this("No-arg constructor");
}
public MyResource2(String response) {
this.response = response;
}
@GET
public String get() {
return this.response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment