To test our API that connects to the facebook graph API we use a mock server setup based on Jersey Test Framework and grizzly:
@Path("/" + PostRest.RESOURCE)
@Produces("application/json")
public class PostRest {
public static final String RESOURCE = "111_222";
@GET
public Response getPost(@QueryParam("access_token") String access_token) {
if (access_token != VALID_TOKEN) {
return Response.status(400).entity(createErrorJson()).build();
}
return Response.status(200).entity(createSomeJsonString()).build();
}
Now while I can react to an invalid or missing access_token with the correct error response, I also want to test that my API reacts correctly when trying to access an unkown resource at facebook ie an unkown path.
Right now I get a 404 from my grizzly obviously, if I try to access say “/111_2”, but facebook seems to catch that error and wrap it inside a Json response, containing the string “false” with status 200.
So… How do I set up the Test Framework to return
Response.status(200).entity("false").build();
every time it is called for an known path?
Basic example:
@ContextConfiguration({ "classpath:context-test.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class SomeTest extends JerseyTest {
@Inject
private SomeConnection connection;
private String unkownId = "something";
public SomeTest() throws Exception {
super("jsonp", "", "com.packagename.something");
}
@Test(expected = NotFoundException.class)
public void testUnkownObjectResponse() throws NotFoundException {
// here it should NOT result in a 404 but a JSON wrapped error response
// which will be handled by the Connection class and
// result in a custom exception
connection.getObject(unkownId);
}
Or maybe I can set up grizzly to behave as desired..?!
Thanks!
Obviously facebook has it own service to intercept errors. Same thing should be done in your code. Just expose you own test service that intercepts all request
This service will produce any response you want. So any un-existing pages like http://mytest/test/errorTrap/111_2 will be intercepted by test service and produce expected response for you