为 RESTLET Web 服务编写测试用例的最佳方法是什么?

发布于 2024-07-22 05:39:16 字数 405 浏览 2 评论 0 原文

我有一个使用 Restlet 库实现的 JAX-RS Web 服务,现在我想测试它。 为了做到这一点,我想通过使用模拟服务预初始化该服务来在我的测试中托管该服务。

托管此类服务并执行测试调用的最佳方法是什么?

@Path("/srv")
public class MyService
{
   @GET
   public void action(@Context UriInfo uri)
   { ... }
}

@Test
public void myTest()
{
   MyService service = new MyService();
   service.setSomething(...);

   // How do I host it?

   // How do I call it?
}

I have a JAX-RS web service implemented with Restlet library and now I want to test it. In order to do that I'd like to host this service in my test by preinitializing it with mocked services.

What is the best way to host such a service and execute the test calls?

@Path("/srv")
public class MyService
{
   @GET
   public void action(@Context UriInfo uri)
   { ... }
}

@Test
public void myTest()
{
   MyService service = new MyService();
   service.setSomething(...);

   // How do I host it?

   // How do I call it?
}

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

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

发布评论

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

评论(2

太阳男子 2024-07-29 05:39:16

@参见http://www.restlet.org/documentation/1.1/firstSteps#part04< /a>

您应该能够以嵌入式方式运行 Restlet 服务并使用 apache HttpClient 来调用方法。 我已经做到了,而且非常简单。

@see http://www.restlet.org/documentation/1.1/firstSteps#part04

You should be able to run the Restlet service in embedded way and use apache HttpClient to call methods. I've done it and it quite simple.

谎言 2024-07-29 05:39:16

Restlet 允许您在各种服务器“连接器”上运行 Web 服务,并且从一台服务器切换到另一台服务器非常容易。 我们通常在 Sun Glassfish Java EE 应用程序服务器集群上运行 Web 服务,但为了测试它们,我们使用与简单 HTTP 服务器链接的连接器来将 Web 服务作为独立应用程序运行。 还有用于 AsyncWeb、Jetty、Grizzly 和内部 HTTP 服务器的服务器连接器。

在客户端,您应该考虑 Restlet 客户端库。 它非常简洁,并且旨在与 Restlet 服务器很好地配合。 我们使用 Apache HTTP 客户端连接器。

为了进行测试,我们创建了 Fetcher 类。 这是使用 Restlet 客户端 API 实现的。 要使用它,您几乎可以调用 fetch() 方法:

DTO person = fetch("/employee/1234");
DTO department = fetch("/department/" + person.getDepartment());

Fetch() 将给定的资源名称附加到 Web 服务的基本 URI 上(例如“http://localhost:8182"),使用 Restlet 客户端 API 获取 XML 表示形式,然后将获取的 XML 反序列化为数据传输对象(PO​​JO)。

您可以看到这确实使单元测试变得非常容易。 在进行单元测试之前,您需要在 Simple 或 Jetty 等独立服务器上启动 Web 服务。 在单元测试期间,您使用 Fetcher 获取 DTO、DOM 树、json.org 对象或其他任何内容,然后将测试断言应用于返回的内容。 如果需要进行更详细的测试,可以直接使用Restlet客户端代码。

Restlet lets you run your web services on various server "connectors", and it is quite easy to switch from one server to another. We normally run our web services on a Sun Glassfish Java EE application server cluster, but for testing them we use a connector that links with the Simple HTTP Server to run the web services as a standalone application. There also are server connectors for AsyncWeb, Jetty, Grizzly, and an internal HTTP server.

On the client side, you should consider the Restlet client library. It's pretty concise and it's designed to mesh well with Restlet servers. We use the Apache HTTP Client connector.

For testing, we've created the Fetcher class. This is implemented using the Restlet client API. To use it, you pretty much call the fetch() method:

DTO person = fetch("/employee/1234");
DTO department = fetch("/department/" + person.getDepartment());

Fetch() tacks the given resource name onto the base URI of the web services (say "http://localhost:8182"), uses the Restlet client API to fetch an XML representation, then deserializes the fetched XML into a data transfer object (a POJO).

You can see that this really makes unit testing quite easy. Before the unit tests, you fire up the web services on a standalone server like Simple or Jetty. During the unit tests you fetch DTOs, DOM trees, json.org objects or whatever using Fetcher, then apply test assertions to to what was returned. If you need to test at a more detailed level, you can use the Restlet client code directly.

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