本文介绍了在不运行 Tomcat 的情况下进行 RestAssured 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要测试的 REST Web 服务.我使用 Mockito 来模拟 DAO 类,使用 RestAssured 来通过 URI 测试 REST 方法.有什么方法可以在不与 Tomcat 单独运行的情况下测试 REST 服务?或者如何在测试用例之前使用模拟类在 Tomcat 上运行应用程序?

I have REST web service which needs to be tested. I am using Mockito for mocking DAO classes and RestAssured for testing REST methods through URI. Is there any way to test REST service without running it separately with Tomcat? Or how to run application on Tomcat with mocked classes before test cases?

推荐答案

有一个教程告诉你如何使用 maven 来启动一个嵌入式的 tomcat 实例并使用 RestAssured 对你的服务运行测试:

There is a tutorial that shows you how to use maven to start an embedded instance of tomcat and run tests against your service using RestAssured:

http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/

您在一个 shell 中启动 tomcat,然后在另一个 shell 中运行测试.

You start tomcat in one shell and run your tests in another.

但是,我强烈建议使用 jersey 测试框架,它可以透明地启动嵌入式容器.在这种情况下,您根本不会使用 RestAssured,而是使用球衣测试客户端.您的测试将运行得更快、更轻松.这里有很好的记录:https://jersey.github.io/documentation/latest/test-framework.html.本教程还演示了这种方法,尽管在我看来客户端的构造并不正确.

However, I strongly recommend using the jersey test framework which transparently spins up an embedded container. In this case you wouldn't use RestAssured at all, but the jersey test client. Your tests will run more quickly and with less fuss. It's well documented here: https://jersey.github.io/documentation/latest/test-framework.html. The tutorial also demonstrates this approach, though it doesn't seem to me that the client is correctly constructed.

过去我也通过直接调用实现类方法来测试 REST 资源.虽然这没有测试 http 查询参数/正文到 java 方法参数的正确映射,但通常就足够了(尤其是当我也在编写客户端代码时).

In the past I've also tested REST resources by calling the implementing class methods directly. Though this doesn't test the correct mapping of the http query parameters/body to java method parameters, it was often sufficient (especially when I'm also coding the client side code).

这篇关于在不运行 Tomcat 的情况下进行 RestAssured 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 19:06