[REQ] New Testing Sub Template - Spring MockMVC
Created by: Crain-32
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());
}
}
``