Create Unit Test Code from Swagger API for Java REST

I tried to use Swagger test patterns and found this an interesting tool for creating test files for my controllers, but it seems to work only for NodeJs projects, because there is no such tool for the Java platform.

Does anyone know how to generate these test files from my controllers using the swagger file for spring boot projects?

PD:

I tried using commercial tools like RepreZen and SwaggerHub, but they do not generate test files for me.

I also tried using the jag tool for script generators to create such files, but this tool just generates code for the client, but not for the server.

You are a lot! ..

+4
source share
3 answers

Erickson

While the RepreZen API Studio does not currently contain a generator template for unit tests, for this you can write your own code generator using the built-in snap-in and code structure.

Further information here: http://docs.reprezen.com/#code-gen

If you want to collaborate on this, please contact us.

Ted Epstein, CEO | Reprezen

+1
source

Erickson

Hello! I am one of the participants in test patterns. This question was asked here , and I answered it there , but I will copy here.

: , Node.js API swagger- node.

: STT, Node.js Node.js, . . Readme , . , API Node.js , STT, HTTP-. Node.js, , (localhost: 1337, my.api.test.net), . , mocha, Node.js runner/framework. Node.js, .

EDIT: , Node.js, Java, , . , , - .

+1

API Spring -family Springfox + AssertJ Swagger. YAML API. - Swagger YAML Swagger JSON, SpringFox.

Springfox Spring MVC Swagger 1.2 Swagger 2.0. Springfox JSON API API, Spring.

Assertj-Swagger - , Swagger YAML Swagger JSON (, springfox), assertj-swagger , .

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
    AssertJ-Swagger SpringFox, SpringFox v2.6.1.
  • Warning! I believe that the consumer contract test is too strict for my project, it gives a lot of false errors, for example. if the actual DTO name is different from the name in the specification.
    I do not know how to deal with this problem. All suggestions are welcome!
+1
source

Source: https://habr.com/ru/post/1655352/


All Articles