5

使用 Restlet,我为我的 Java 应用程序创建了一个路由器。

通过使用 curl,我知道每个不同的 GET、POST 和 DELETE 请求都适用于每个 URI,并返回正确的 JSON 响应。

我想为每个 URI 设置 JUnit 测试,以简化测试过程。但是,我不确定向每个 URI 发出请求以获得 JSON 响应的最佳方法,然后我可以比较它以确保结果符合预期。关于如何做到这一点的任何想法?

4

4 回答 4

7

您可以只使用 RestletClient发出请求,然后检查每个响应及其表示。

例如:

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

实际上,我对 JUnit 或一般的单元测试并不熟悉assert,所以如果我的示例有问题,我深表歉意。希望它仍然说明了一种可能的测试方法。

祝你好运!

于 2010-02-24T04:58:37.140 回答
3

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

router和资源在我的测试类中是@Autowired 。Spring 应用程序上下文中的相关声明如下所示

<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" />

并且myApplication类似于

public class MyApplication extends Application {
}
于 2012-07-02T07:44:09.217 回答
1

我得到了 REST junit 测试用例中挑战响应设置的答案

@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());
}
于 2014-03-11T05:08:56.567 回答
0

根据Avi Flax 的回答,我将此代码重写为 java 并使用junitparams运行它,这是一个允许通过参数化测试的库。代码如下所示:

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

    }
}
于 2012-12-27T12:42:31.860 回答