简体   繁体   English

从Swagger API for Java REST生成单元测试代码

[英]Generate Unit Test Code from Swagger API for Java REST

I was trying using Swagger test templates and found this an interesting tool to generate the test files for my controllers, but It seems to just work for NodeJs projects, because there is not a similar tool for the Java platform. 我正在尝试使用Swagger测试模板并发现这是一个有趣的工具来为我的控制器生成测试文件,但它似乎只适用于NodeJs项目,因为Java平台没有类似的工具。

Do somebody knows about how to generate these Test files from my controllers by using a swagger file for spring boot projects? 有人知道如何通过使用swagger文件为我的控制器生成这些测试文件的春季启动项目?

PD: PD:

I have tried with commercial tools like RepreZen and SwaggerHub but they don't generate the test files for me. 我尝试使用像RepreZen和SwaggerHub这样的商业工具,但它们不会为我生成测试文件。

I have also tried by using swagger-generator jar tool to generate those kind of files, but this tool just generate code for Client but no for the Server. 我也试过使用swagger-generator jar工具来生成那种文件,但是这个工具只为Client生成代码但是没有为Server生成代码。

Thx you a lot!. 你好多了!

Erikson, 埃里克森,

While RepreZen API Studio doesn't currently include a generator template for unit tests, you can write a custom code generator for this purpose, using the built-in tooling and code gen framework. 虽然RepreZen API Studio当前不包含用于单元测试的生成器模板,但您可以使用内置工具和代码框架为此目的编写自定义代码生成器。

More info here: http://docs.reprezen.com/#code-gen 更多信息: http//docs.reprezen.com/#code-gen

If you'd like to collaborate on this, please feel free to get in touch with us. 如果您想就此进行合作,请随时与我们联系。

Ted Epstein, CEO | 首席执行官Ted Epstein RepreZen RepreZen

For testing API of Spring-family projects you can use Springfox + AssertJ Swagger libraries. 要测试Spring-family项目的API,可以使用Springfox + AssertJ Swagger库。 Also you need a YAML file with your API specification. 您还需要一个包含API规范的YAML文件。 The main idea of this solution is to compare a contract-first Swagger YAML file with a code-first Swagger JSON generated by SpringFox. 此解决方案的主要思想是将契约优先Swagger YAML文件与SpringFox生成的代码优先Swagger JSON进行比较。

Springfox integrates with Spring MVC with support for Swagger 1.2 and Swagger 2.0 spec. Springfox与Spring MVC集成,支持Swagger 1.2和Swagger 2.0规范。 Springfox is able to automatically generate JSON API documentation at runtime for API's built with Spring. Springfox能够在运行时为使用Spring构建的API自动生成JSON API文档。

Assertj-Swagger is a library which compares a design-first Swagger YAML with an implementation-first Swagger JSON output (eg from springfox). Assertj-Swagger是一个库,它将设计优先的Swagger YAML与实现优先的Swagger JSON输出(例如来自springfox)进行比较。 assertj-swagger allows to validate that the implementation in compliance with the design specification. assertj-swagger允许验证实现是否符合设计规范。

Project setup example 项目设置示例

pom.xml 的pom.xml

<!-- http://springfox.io -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.github.robwin</groupId>
    <artifactId>assertj-swagger</artifactId>
    <version>0.6.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.8.0</version>
    <scope>test</scope>
</dependency>

Test code: 测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AssertJSwaggerConsumerDrivenTest {

    @LocalServerPort
    int randomPort;

    @Test
    public void validateThatImplementationSatisfiesConsumerSpecification() {
        File designFirstSwagger = new File(AssertJSwaggerConsumerDrivenTest.class.getResource("/swagger.yaml").getFile());
        SwaggerAssertions.assertThat("http://localhost:" + randomPort + "/v2/api-docs")
                .satisfiesContract(designFirstSwagger.getAbsolutePath());
    }    
}

Troubleshooting 故障排除

  • Exception java.lang.UnsupportedClassVersionError: io/github/robwin/swagger/test/SwaggerAssertions : Unsupported major.minor version 52.0 异常java.lang.UnsupportedClassVersionError: io/github/robwin/swagger/test/SwaggerAssertions : Unsupported major.minor version 52.0
    Solution: Use JRE 1.8+ 解决方案:使用JRE 1.8+
  • Exception java.lang.NoSuchMethodError: io.swagger.models.parameters.AbstractSerializableParameter.setMaximum(Ljava/lang/Double;)V 异常java.lang.NoSuchMethodError: io.swagger.models.parameters.AbstractSerializableParameter.setMaximum(Ljava/lang/Double;)V
    At the moment of writing AssertJ-Swagger library is incompatible with the latest SpringFox release, so I recommend to use SpringFox v2.6.1. 在编写AssertJ-Swagger库的时刻与最新的SpringFox版本不兼容 ,所以我建议使用SpringFox v2.6.1。
  • Warning! 警告! I find Consumer Driven Contract test too strict for my project, it gives a lot of false errors, eg if an actual DTO name differs from a name in the specification. 我发现Consumer Driven Contract测试对我的项目来说过于严格,它会产生很多错误,例如,如果实际的DTO名称与规范中的名称不同。
    I don't know how to fight this issue. 我不知道如何解决这个问题。 All suggestions are welcomed! 欢迎所有建议!

Erikson, 埃里克森,

Hi! 嗨! I'm one of the contributors on swagger-test-templates. 我是swagger-test-templates的贡献者之一。 This question was asked here as well, and I answered it there , but I will copy here. 这个问题也在这里被问到,我在那里回答了 ,但我会在这里复制。

Simple answer: No, it is not exclusively for Node.js APIs or swagger-node. 简单回答:不,它不仅仅适用于Node.js API或swagger-node。

Longer answer: You can use STT like any other Node.js module in a Node.js project, completely on its own. 更长的答案:您可以像Node.js项目中的任何其他Node.js模块一样使用STT,完全靠它自己。 See the Readme or the test files for an example of how to run it standalone. 有关如何独立运行它的示例,请参阅自述文件或测试文件。 Furthermore, the API doesn't have to be implemented in Node.js for a STT generated test to target it with an HTTP request. 此外,不必在Node.js中实现API,以便STT生成的测试使用HTTP请求来定位它。 You have to use Node.js to utilize this module's functionality, but as long as you point the tests at a running server (localhost:1337, my.api.test.net) the backend implementation doesn't matter. 您必须使用Node.js来利用此模块的功能,但只要您将测试指向正在运行的服务器(localhost:1337,my.api.test.net),后端实现就无关紧要了。 The caveat here is that this module was designed to run with mocha which is a Node.js test runner/framework. 需要注意的是,此模块设计为使用mocha运行, mocha是Node.js测试运行器/框架。 So the tests have to be in a Node.js project, but the server implementation doesn't. 因此测试必须在Node.js项目中,但服务器实现不是。

EDIT: That^ being said, it does generate Node.js code, not Java code, if that is what you were looking for. 编辑:那就是说,它确实生成了Node.js代码,而不是Java代码,如果那是你想要的。 Please follow up if I can be answer anything more. 请跟进,如果我可以回答任何问题。

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

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