java - 有没有办法使用 swagger codegen 自动生成 java 测试代码?
问题描述
我一直在为 springboot 项目使用 swagger codegen。我现在能够在 java 中自动生成客户端代码,忽略数据模型的生成(通过 importMapping 在 swagger-codegen-maven-plugin 中导入我自己的)。此外,我还能够:
- 直接在springboot代码中指定标签,帮助组织openapi合约,
- 使用@Operation 注解指定方法名,
- 使用@ApiResponses 注释来详细说明预期的响应代码和描述等。
下面的例子:
@RestController
@RequestMapping(value="/api/external/")
@Tag(name = "addResources", description = "The Add Resources API")
public class ControllerAddResources {
private final ServiceInterAddResources externalSources;
public ControllerAddResources(
ServiceInterAddResources externalSources){
this.externalSources = externalSources;
}
@Operation(operationId="importDataV3", summary = "Import Data V3", description = "Import a Data V3 file from source", tags = { "addResources" })
@PostMapping(path="/{source}/datav3", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public @ResponseBody String importDataV3(
@PathVariable String source,
MultipartFile file) throws ExceptionInvalidDataV3, IOException{
return externalSources.importDataV3(source, file);
}
所有这些都内置在 openapi 合约中,然后 codegen 使用该合约来生成客户端库(在这种情况下,我使用的是 resttemplate),例如下面的一个:
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-03-05T12:15:11.537Z[Europe/Lisbon]")@Component("com.xxx.connector.api.AddResourcesApi")
public class AddResourcesApi {
private ApiClient apiClient;
public AddResourcesApi() {
this(new ApiClient());
}
@Autowired
public AddResourcesApi(ApiClient apiClient) {
this.apiClient = apiClient;
}
public ApiClient getApiClient() {
return apiClient;
}
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
/**
* Import Data V3
* Import a Data V3 file from source
* <p><b>412</b> - default response
* <p><b>409</b> - default response
* <p><b>200</b> - default response
* @param source The source parameter
* @param file The file parameter
* @return String
* @throws RestClientException if an error occurs while attempting to invoke the API
*/
public String importDataV3(String source, File file) throws RestClientException {
Object postBody = null;
// verify the required parameter 'source' is set
if (source == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'source' when calling importDataV3");
}
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("source", source);
String path = UriComponentsBuilder.fromPath("/api/external/{source}/datav3").buildAndExpand(uriVariables).toUriString();
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
if (file != null)
formParams.add("file", new FileSystemResource(file));
final String[] accepts = {
"*/*", "application/json"
};
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
"multipart/form-data"
};
final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
String[] authNames = new String[] { };
ParameterizedTypeReference<String> returnType = new ParameterizedTypeReference<String>() {};
return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
在生成的源代码中,codegen 还包括用于 junit 测试的脚手架,如下所示:
/**
* API tests for AddResourcesApi
*/
@Ignore
public class AddResourcesApiTest {
private final AddResourcesApi api = new AddResourcesApi();
/**
* Import Data V2
*
* Import an Data V2 file from source
*
* @throws ApiException
* if the Api call fails
*/
@Test
public void imporDataV2Test() {
String source = null;
File file = null;
String response = api.importDataV2(source, file);
// TODO: test validations
}
/**
* Import Data V3
*
* Import an Data V3 file from source [%source%]
*
* @throws ApiException
* if the Api call fails
*/
@Test
public void importDataV3Test() {
String source = null;
File file = null;
String response = api.importDataV3(source, file);
// TODO: test validations
}
}
但是,正如预期的那样,验证码是空的。由于客户端在 CI/CD 环境中不断生成并部署到我在内部运行的依赖项管理系统(Artifactory),这违背了每次执行 codegen 时手动编写测试的目的。
有没有办法在springboot项目级别直接使用java注释(或模板机制)指定验证代码(或验证先决条件)?这将允许完全自动化的客户端库生成,包括测试。
解决方案
推荐阅读
- django - Django:为什么我的代码让用户使用已被占用的电子邮件地址创建一个新帐户?
- python - 如何使用给定的列表位置设置嵌套 python dict 的“第 n 个”元素?
- java - 如何使用 Android Studio 根据其在 Firebase 中的值删除子项
- css - 是否可以指向一个类?
- asp.net-mvc - 将数据从 Excel 导入 MVC
- java - 有没有办法在 android 上使用 opengles 设置和绘制每个像素?
- android - 使用堆栈导航回片段
- javascript - 使用 `node` 启动节点应用程序有效,但在使用 `/usr/bin/node` 启动它时无效
- amazon-web-services - MUP 部署卡住
- javascript - 如何用数据库填充在 javascript 中创建的选择?