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

Generate Unit Test Code from Swagger API for Java REST

我尝试使用 Swagger 测试模板,发现这是一个有趣的工具,可以为我的控制器生成测试文件,但它似乎只适用于 NodeJs 项目,因为没有类似的工具用于 Java 平台.

有人知道如何为 spring 引导项目使用 swagger 文件从我的控制器生成这些测试文件吗?

PD:

我已经尝试使用 RepreZen 和 SwaggerHub 等商业工具,但它们不会为我生成测试文件。

我也尝试过使用 swagger-generator jar 工具来生成这些文件,但是这个工具只为客户端生成代码而没有为服务器生成代码。

非常感谢你!

埃里克森,

虽然 RepreZen API Studio 当前不包含用于单元测试的生成器模板,但您可以使用内置工具和代码生成框架为此目的编写自定义代码生成器。

更多信息在这里:http://docs.reprezen.com/#code-gen

如果您想就此进行合作,请随时与我们联系。

泰德·爱泼斯坦,首席执行官 |代表禅

埃里克森,

嗨!我是 swagger-test-templates 的贡献者之一。有人问这个问题 here as well, and I answered it there,但我会复制到这里。

Simple answer: No, it is not exclusively for Node.js APIs or swagger-node.

Longer answer: You can use STT like any other Node.js module in a Node.js project, completely on its own. 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. 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. The caveat here is that this module was designed to run with mocha which is a Node.js test runner/framework. So the tests have to be in a Node.js project, but the server implementation doesn't.

编辑: 话虽这么说,它确实会生成 Node.js 代码,而不是 Java 代码,如果那是您正在寻找的代码。如果我能再回答什么,请跟进。

要测试 API 的 Spring 系列项目,您可以使用 Springfox + AssertJ Swagger 库。您还需要一个包含 API 规范的 YAML 文件。该解决方案的主要思想是将合同优先的 Swagger YAML 文件与 SpringFox.

生成的代码优先的 Swagger JSON 进行比较。

Springfox integrates with Spring MVC with support for Swagger 1.2 and Swagger 2.0 spec. Springfox is able to automatically generate JSON API documentation at runtime for API's built with Spring.

Assertj-Swagger is a library which compares a design-first Swagger YAML with an implementation-first Swagger JSON output (e.g. from springfox). assertj-swagger allows to validate that the implementation in compliance with the design specification.

项目设置示例

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>

测试代码:

@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());
    }    
}

疑难解答

  • 异常java.lang.UnsupportedClassVersionError: io/github/robwin/swagger/test/SwaggerAssertions : Unsupported major.minor version 52.0
    解决方案:使用 JRE 1.8+
  • 异常java.lang.NoSuchMethodError: io.swagger.models.parameters.AbstractSerializableParameter.setMaximum(Ljava/lang/Double;)V
    在用最新的 SpringFox 版本编写 AssertJ-Swagger 库 is incompatible 的时候,所以我建议使用 SpringFox v2.6.1.
  • 警告!我发现消费者驱动契约测试对我的项目来说太严格了,它给出了很多假错误,例如如果实际的 DTO 名称与规范中的名称不同。
    我不知道如何解决这个问题。欢迎所有建议!