在 Restlet 路由器上运行 JUnit 测试

发布于 2024-08-23 05:37:07 字数 236 浏览 8 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2024-08-30 05:37:07

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

例如:

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 或一般的单元测试,所以如果我的示例有问题,我深表歉意。希望它仍然说明了一种可能的测试方法。

祝你好运!

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!

白首有我共你 2024-08-30 05:37:07

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

其中路由器和资源在我的测试类中是@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 {
}

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 {
}
恏ㄋ傷疤忘ㄋ疼 2024-08-30 05:37:07

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

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());
}
病毒体 2024-08-30 05:37:07

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

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文