5

Using Restlet I have created a router for my Java application.

From using curl, I know that each of the different GET, POST & DELETE requests work for each of the URIs and return the correct JSON response.

I'm wanting to set-up JUnit tests for each of the URI's to make the testing process easier. However, I'm not to sure the best way to make the request to each of the URIs in order to get the JSON response which I can then compare to make sure the results are as expected. Any thoughts on how to do this?

Lee
  • 564
  • 1
  • 10
  • 30
  • I had a similar question http://stackoverflow.com/questions/2165561/ways-to-test-restful-services . rest-client should work quite good for your scenario. – Daff Feb 22 '10 at 15:15
  • It's close, but not quite what I'm after. It would be nice if I could set up test-suites etc. Also leads to the problem of all members of the team needing to have access to that UI. – Lee Feb 22 '10 at 15:36

4 Answers4

7

You could just use a Restlet Client to make requests, then check each response and its representation.

For example:

Client client = new Client(Protocol.HTTP);
Request request = new Request(Method.GET, resourceRef);
Response response = client.handle(request);

assert response.getStatus().getCode() == 200;
assert response.isEntityAvailable();
assert response.getEntity().getMediaType().equals(MediaType.TEXT_HTML);

// Representation.getText() empties the InputStream, so we need to store the text in a variable
String text = response.getEntity().getText();
assert text.contains("search string");
assert text.contains("another search string");

I'm actually not that familiar with JUnit, assert, or unit testing in general, so I apologize if there's something off with my example. Hopefully it still illustrates a possible approach to testing.

Good luck!

Avi Flax
  • 50,872
  • 9
  • 47
  • 64
  • That was great. With assert it's assertTrue(...) for anyone else using the example, but perfect apart from that. Thanks – Lee Feb 25 '10 at 15:36
  • My pleasure, glad to help! BTW I recommend you try Groovy for this sort of thing — it makes the tests more concise. It's especially great that it has getter and setter shortcuts, and == means value equality. So instead of response.getEntity().getMediaType().equals(MediaType.TEXT_HTML) you can write response.entity.mediaType == MediaType.TEXT_HTML. HTH! – Avi Flax Feb 25 '10 at 18:59
3

Unit testing a ServerResource

// Code under test
public class MyServerResource extends ServerResource {
  @Get
  public String getResource() {
    // ......
  }
}

// Test code
@Autowired
private SpringBeanRouter router;
@Autowired
private MyServerResource myServerResource;

String resourceUri = "/project/1234";
Request request = new Request(Method.GET, resourceUri);
Response response = new Response(request);
router.handle(request, response);
assertEquals(200, response.getStatus().getCode());
assertTrue(response.isEntityAvailable());
assertEquals(MediaType.TEXT_PLAIN, response.getEntity().getMediaType());
String responseString = response.getEntityAsText();
assertNotNull(responseString);

where the router and the resource are @Autowired in my test class. The relevant declarations in the Spring application context looks like

<bean name="router" class="org.restlet.ext.spring.SpringBeanRouter" />
<bean id="myApplication" class="com.example.MyApplication">
  <property name="root" ref="router" />
</bean> 
<bean name="/project/{project_id}" 
      class="com.example.MyServerResource" scope="prototype" autowire="byName" />

And the myApplication is similar to

public class MyApplication extends Application {
}
user799188
  • 13,965
  • 5
  • 35
  • 37
1

I got the answer for challenge response settings in REST junit test case

@Test
public void test() {
    String url ="http://localhost:8190/project/user/status";
    Client client = new Client(Protocol.HTTP);
    ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC,"user", "f399b0a660f684b2c5a6b4c054f22d89");
    Request request = new Request(Method.GET, url);
    request.setChallengeResponse(challengeResponse);
    Response response = client.handle(request);
    System.out.println("request"+response.getStatus().getCode());
    System.out.println("request test::"+response.getEntityAsText());
}
Aravind Cheekkallur
  • 3,157
  • 6
  • 27
  • 41
0

Based on the answer of Avi Flax i rewrite this code to java and run it with junitparams, a library that allows pass parametrized tests. The code looks like:

@RunWith(JUnitParamsRunner.class)
public class RestServicesAreUpTest {

    @Test
    @Parameters({
        "http://url:port/path/api/rest/1, 200, true",
        "http://url:port/path/api/rest/2, 200, true", })
    public void restServicesAreUp(String uri, int responseCode,
        boolean responseAvaliable) {
    Client client = new Client(Protocol.HTTP);
    Request request = new Request(Method.GET, uri);
    Response response = client.handle(request);

    assertEquals(responseCode, response.getStatus().getCode());
    assertEquals(responseAvaliable, response.isEntityAvailable());
    assertEquals(MediaType.APPLICATION_JSON, response.getEntity()
        .getMediaType());

    }
}
Community
  • 1
  • 1
Francisco Puga
  • 23,869
  • 5
  • 48
  • 64