在不运行Tomcat的情况下进行RestAssured测试 [英] RestAssured testing without running Tomcat

查看:125
本文介绍了在不运行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:

您在一个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测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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