English 中文(简体)
Running JUnit Tests on a Restlet Router
原标题:

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?

最佳回答

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!

问题回答

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

    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签