OpenAPITools / OpenAPITools/openapi-generator

[REQ] New Testing Sub Template - Spring MockMVC

Open
#14,804 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

No current problem besides the lack of clear support.

Note: This is a clone of Issue, given the age of the Issue I figured a new Issue would be preferred.

Describe the solution you'd like

With Spring Framework 6, no mention has been made to deprecate MockMVC as a Spring Server Testing Unit.

As such I think adding this will allow for better generated Tests for users.

Describe alternatives you've considered

I have not considered any alternatives, given how OpenAPI-Generator is bundled into IntelliJ, I'm interested in getting this feature in, as it's the main IDE for myself/my coworkers.

Additional context

Given the standard for small/simple PRs, my plan for this Issue is to just get it to generate the following.

Given the following API Doc,

openapi: 3.0.1
info:
  title: Title
  description: Title
  version: 1.0.0
tags:
  - name: path
    description: Links to Path Controller
paths:
  /rest/path/one:
    get:
      summary: Example Get
      description: Does a Get
      tags:
        - path
      parameters:
        - name: get
          in: query
          required: true
          schema:
            type: string
          description: Query Thing
      responses:
        200:
          description: Get Good
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Response'
        204:
          description: No Get

  /rest/path/two:
    post:
      summary: Example Post
      description: Does a Post
      tags:
        - path
      requestBody:
        description: Post Body.
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Request'
      responses:
        200:
          description: Required
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Response'
components:
  schemas:
    Request:
      properties:
        message:
          description: Message to Send
          type: string
      required:
        - message
    Response:
      properties:
        message:
          description: Response Message
          type: string
      required:
        - message

It currently produces the following API test,

@Ignore
public class PathApiTest {
    private final PathApi api = new PathApi();
    /**
     * Example Get
     *
     * Does a Get
     *
     * @throws ApiException
     *          if the Api call fails
     */
    @Test
    public void restPathOneGetTest() throws ApiException {
        String get = null;
        Response response = api.restPathOneGet(get);

        // TODO: test validations
    }
    /**
     * Example Post
     *
     * Does a Post
     *
     * @throws ApiException
     *          if the Api call fails
     */
    @Test
    public void restPathTwoPostTest() throws ApiException {
        Request request = null;
        Response response = api.restPathTwoPost(request);

        // TODO: test validations
    }
}

I propose with the new Sub-Template spring-mockmvc, the following would get generated.

@Ignore
@WebMVCTest // Please Select your PathController
public class PathMockMVCTest {
   @Autowired
   MockMvc mockMvc;
    /**
     * Example Get
     *
     * Does a Get
     *
     * @throws Exception
     *          if the Api call fails
     */
    @Test
    public void restPathOneGetTest() throws Exception {
        // TODO: test validations
    }
    /**
     * Example Post
     *
     * Does a Post
     *
     * @throws ApiException
     *          if the Api call fails
     */
    @Test
    public void restPathTwoPostTest() throws Exception {
        // TODO: test validations
    }
}

I hope to do follow up PRs so we could get the code gen close to what Spring has in its tutorial, for this Doc it would look something like the following. (Only doing the first Path)

@Ignore
@WebMvcTest(PathController.class)
public class WebMockTest {

    @Autowired
     private MockMvc mockMvc;
     
     final String restPathOne = "/rest/path/one";
     /**
     * Example Get
     *
     * Does a Get
     *
     * @throws Exception
     *          if the Api call fails
     */
    @Test
    public void restPathOneGetTest() throws Exception {
        this.mockMvc.perform(get(restPathOne))
          .andExpect(status().isOk())
          .andExpect(header().string("Content-Type", "application/json")
          .andExpect(jsonPath("$.message").exists());
    }
}
``

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing Spring server test templates and the generated API test structure described in the issue, then compare them with the proposed spring-mockmvc output. The work is done when the new sub-template generates the requested MockMvc test class, including the shown annotations, injected MockMvc field, and operation test methods, with the generator's relevant tests passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.