简体   繁体   English

Spring启动REST应用程序测试方法

[英]Spring boot REST application testing approach

I have a Spring boot + REST application. 我有一个Spring启动+ REST应用程序。 When I need to write unit testing, should I directly invoke the service beans or call the rest controller? 当我需要编写单元测试时,我应该直接调用服务bean还是调用其余的控制器? If I invoke the rest controller directly, I have to use RestTemplate and invoke the rest api as a client, right? 如果我直接调用其余的控制器,我必须使用RestTemplate并调用其余的api作为客户端,对吧?

What would be the best and required practice? 什么是最好的和必要的练习?

If I invoke the service beans directly it will result in less code coverage because controller methods code will be not covered. 如果我直接调用服务bean,它将导致更少的代码覆盖率,因为控制器方法代码将不会被覆盖。 Is that acceptable? 那可以接受吗?

Hmm, this is a complex question but I'll answer as best I can. 嗯,这是一个复杂的问题,但我会尽我所能回答。 A lot of this will depend on you/your organization's risk tolerance and how much time they want to invest in tests. 这很大程度上取决于您/您组织的风险承受能力以及他们希望投入测试的时间。 I believe in a lot of testing, but there is such a thing as too much. 我相信很多测试,但有太多这样的事情。

A unit test tests the unit of code. 单元测试测试代码单元。 Great, but what's a unit? 很棒,但是什么是单位? This article is a pretty good discussion: http://martinfowler.com/bliki/UnitTest.html but a unit is basically the smallest testable part of your application. 这篇文章是一个非常好的讨论: http//martinfowler.com/bliki/UnitTest.html但是一个单元基本上是你应用程序中最小的可测试部分。

Much literature (eg https://www.amazon.ca/Continuous-Delivery-Reliable-Deployment-Automation/dp/0321601912/ ) describes multiple phases of testing including unit tests which are very low level and mock externalities such as DBs or file systems or remote systems, and "api acceptance tests" (sometimes called integration tests although this is a vague term that can mean other things). 许多文献(例如https://www.amazon.ca/Continuous-Delivery-Reliable-Deployment-Automation/dp/0321601912/ )描述了多个测试阶段,包括非常低级别的单元测试和模拟外部性,如DB或文件系统或远程系统,以及“api验收测试”(有时称为集成测试,尽管这是一个含糊不清的术语,可能意味着其他事情)。 This latter type fires up a test instance of your application, invokes APIs and asserts on responses. 后一种类型会激活应用程序的测试实例,调用API并对响应进行断言。

The short answer is as follows: for unit tests, focus on the units (probably services or more granular), but the other set of tests you describe, wherein the test behaves like a client and invokes your api, are worthwhile too. 简短的回答如下:对于单元测试,关注单元(可能是服务或更细粒度),但是您描述的另一组测试,其中测试行为类似于客户端并调用您的API,也是值得的。 My suggestion: do both, but don't call both unit tests. 我的建议:做两件事,但不要同时进行单元测试。

Best Approach is to Test VIA Controllers. 最佳方法是测试VIA控制器。 WebServices are entered and values are returned here. 输入Web服务并在此处返回值。 So Controller is having quite a good role in this. 因此,控制器在这方面发挥了相当大的作用。 There can be small logic as well, you may miss that 也可能有小逻辑,你可能会错过

You can Try Using the MockMvc Method for testing controllers. 您可以尝试使用MockMvc方法来测试控制器。

Reference: Reference-1 , Reference-2 参考文献: 参考文献-1参考文献-2

Or use the RestTemplate as you mentioned in question Reference-3 或者使用问题参考文献3中提到的RestTemplate

它基于您想要测试的内容,您可以分离您的测试,特别是如果您有开发人员团队,测试用例来测试您的业务“服务”,另一个测试用例作为集成测试来使用REST模板,在这种情况下你可以更快更容易地找出你的错误。

It depends on what you want to do. 这取决于你想做什么。

One approach would be to unit test the units of work, like the service and the MVC controller. 一种方法是对工作单元进行单元测试,例如服务和MVC控制器。 These test will only test eventual logic found in this classes and try to reach a high branch coverage, if applicable. 这些测试仅测试此类中的最终逻辑,并尝试达到高分支覆盖(如果适用)。 Besides this, you can write an integration test that makes the HTTP request, goes to the real service bean and only mock an eventual resource access. 除此之外,您还可以编写一个集成测试来发出HTTP请求,转到真正的服务bean,并且只模拟最终的资源访问。

For integration tests you can use Spring's support, see here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#spring-mvc-test-framework 对于集成测试,您可以使用Spring的支持,请参见此处: http//docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#spring-mvc-test-framework

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM