无需运行 Tomcat 的 RestAssured 测试 [英] RestAssured testing without running Tomcat

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

问题描述

我有需要测试的 REST Web 服务.我使用 Mockito 来模拟 DAO 类,使用 RestAssured 通过 URI 测试 REST 方法.有没有办法测试REST服务而不用Tomcat单独运行它?或者如何在测试用例之前使用模拟类在 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,而是使用 jersey 测试客户端.您的测试将运行得更快、更省心.这里有详细记录: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 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆