OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[REQ] A configuration of the JUnit version used for the tests produced by the Java generator #16352

Open Pallau opened 1 year ago

Pallau commented 1 year ago

Is your feature request related to a problem? Please describe.

We are using the Java generator in order to access other REST resources. The generated code also produces JUnit tests (a nice feature to keep our test coverage threshold) but they are designed to use the JUnit 4 library. In our case we only use JUnit 5. At the moment there is no possibility to configure this.

Describe the solution you'd like

The openapi-generator-maven-plugin has already the configuration to turn off the API tests and Model tests (configuration parameters: generateApiTests and generateModelTests). Another configuration to determine the utilized JUnit library in these tests would be nice.

Describe alternatives you've considered

Maybe to supersede the default case of now using JUnit 5 instead of the old JUnit 4 library.

Additional context

In the beginning I have asked at Stackoverflow to get some help and somone mentioned to create a Feature Request here: https://stackoverflow.com/questions/76920305/generated-test-classes-use-junit-4

wing328 commented 1 year ago

The generated code also produces JUnit tests (a nice feature to keep our test coverage threshold) but they are designed to use the JUnit 4 library

which java generator(s) are you using?

I think some have been upgraded to generated test code with junit5 instead.

we welcome PR to update the auto-generated code to use junit5

Pallau commented 1 year ago

which java generator(s) are you using?

This one! (current openapi-generator version 6.6.0)

wing328 commented 1 year ago

I just did a test and java (default library: okhttp-gson) generator outputs code that uses junit 5.x:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/ping.yaml  -o /tmp/java-test/

In pom.xml

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <gson-fire-version>1.8.5</gson-fire-version>
        <okhttp-version>4.10.0</okhttp-version>
        <gson-version>2.9.1</gson-version>
        <commons-lang3-version>3.12.0</commons-lang3-version>
        <jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
        <jakarta-annotation-version>1.3.5</jakarta-annotation-version>
        <junit-version>5.9.1</junit-version>
        <junit-platform-runner.version>1.9.1</junit-platform-runner.version>
        <mockito-core-version>3.12.4</mockito-core-version>
        <javax.ws.rs-api-version>2.1.1</javax.ws.rs-api-version>
        <jsr311-api-version>1.1.1</jsr311-api-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spotless.version>2.27.2</spotless.version>
    </properties>
Pallau commented 1 year ago

We're using webclient as the library. This produces JUnit 4 tests.

wing328 commented 1 year ago

can you please file a PR to help update webclient related templates to use Junit 5? let me know if you need some good starting points.

Pallau commented 1 year ago

I want you to show still the Maven plugin configuration we have:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.6.0</version>
    <executions>
        <execution>
            <id>stammdaten-verwaltung-application-client-codegen</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/stammdatenverwaltung-v1.yaml</inputSpec>
                <generatorName>java</generatorName>
                <generateApiTests>false</generateApiTests> <!-- We need this because the tests are JUnit 4 ones -->
                <generateModelTests>false</generateModelTests> <!-- We need this because the tests are JUnit 4 ones -->
                <configOptions>
                    <apiPackage>de.stammdaten.generated.api.v1</apiPackage>
                    <modelPackage>de.stammdaten.generated.model.v1</modelPackage>
                    <useJakartaEe>true</useJakartaEe>
                    <library>webclient</library>
                    <dateLibrary>java8-localdatetime</dateLibrary>
                    <implicitHeadersRegex>X-Correlation-Id</implicitHeadersRegex>
                    <additionalModelTypeAnnotations>
                        @lombok.Builder
                        @lombok.AllArgsConstructor
                    </additionalModelTypeAnnotations>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

can you please file a PR to help update webclient related templates to use Junit 5? let me know if you need some good starting points.

I will give it a try.

timo-abele commented 9 months ago

We are having the same issue with resttemplate. As can be seen here https://github.com/OpenAPITools/openapi-generator/blob/783e68c7acbbdcbb2282d167d1644b069f12d486/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api_test.mustache#L7-L8 they are also still using Junit4.

wing328 commented 9 months ago

@timo-abele can you please file a PR to update junit to newer version in the resttemplate?

argem commented 6 months ago

@timo-abele can you please file a PR to update junit to newer version in the resttemplate?

Same for 'rest-assured' library https://github.com/OpenAPITools/openapi-generator/blob/fde8c772fb9442b31bd348f7c0847f56f1df7631/modules/openapi-generator/src/main/resources/Java/libraries/rest-assured/api_test.mustache#L11

Could you please fix it too

timo-abele commented 6 months ago

I'm afraid I won't find the time to do this any time soon @argem, but I hope my PR #18222 can serve as a starting point if you want to tackle this yourself.

flozano commented 1 month ago

after upgrading from 6.6 to 7.9, I am getting junit 5 tests, whereas my project uses junit 4... is there any way to go back to generating junit4 tests?

wing328 commented 1 month ago

@flozano one way is to customize the templates to do so (e.g. -t via CLI)

or simply skip generating the test files: https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#ignore-file-format