openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open Pallau opened this issue 2 years ago • 10 comments

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

Pallau avatar Aug 18 '23 07:08 Pallau

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

wing328 avatar Aug 18 '23 11:08 wing328

which java generator(s) are you using?

This one! (current openapi-generator version 6.6.0)

Pallau avatar Aug 18 '23 20:08 Pallau

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>

wing328 avatar Aug 19 '23 02:08 wing328

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

Pallau avatar Aug 20 '23 15:08 Pallau

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.

wing328 avatar Aug 20 '23 16:08 wing328

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.

Pallau avatar Aug 21 '23 17:08 Pallau

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.

timo-abele avatar Feb 08 '24 09:02 timo-abele

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

wing328 avatar Feb 08 '24 09:02 wing328

@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

argem avatar May 05 '24 01:05 argem

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.

timo-abele avatar May 06 '24 14:05 timo-abele