Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

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 {
}

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());
}

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());

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top