【问题标题】:Swagger api client methods return voidSwagger api 客户端方法返回 void
【发布时间】:2018-01-24 08:40:00
【问题描述】:

Swagger api 客户端,由 codegen 工具生成,只创建返回类型为 void 的测试方法。如何测试我的 rest api?

这是我的服务代码规范:

 @Api(value = "Authentication Recovery")
 @Path("/authenticationRecovery")

public class AuthenticationRecoveryResource implements Authentication{

@ApiOperation(
        value = "Recover access token"
        , notes = "Recover access token assigned to the user (once it has been authenticated)"
        , response = TokenJAXB.class
        //, responseContainer = "List"
)
@ApiResponses(value = { 
        @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Authorized access") ,
        @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "Unauthorized access") 
})
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response authenticate(
        @ApiParam(value="Username", required=true) @FormParam("username") String username,
        @ApiParam(value="Password", required=true) @FormParam("password") String password) 
{..}

这是我生成的用于测试的招摇代码:

/**
  * API tests for AuthenticationRecoveryApi
*/
@Ignore

public class AuthenticationRecoveryApiTest {

private final AuthenticationRecoveryApi api = new AuthenticationRecoveryApi();

/**
 * Recover access token
 *
 * Recover access token assigned to the user (once it has been authenticated)
 *
 * @throws ApiException
 *             if the Api call fails
 */
@Test
public void authenticateTest() throws ApiException {
    String username = null;
    String password = null;
    api.authenticate(username, password);

    // TODO: test validations
}}

我已经生成了一个招摇的客户端 api:

java -jar swagger-codegen-2.2.3/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i http://localhost:8080/myproject/services/service0/swagger.json -l java -o client/myproject/java

【问题讨论】:

  • 你能发布你的规格吗?您生成了哪种语言/框架?

标签: java api testing swagger codegen


【解决方案1】:

Swagger CodegenJava 生成器尽其所能 - 它具有带有函数的测试代码(更像是函数存根),您可以在其中填写要传递给 API 端点的参数值,例如作为上面的变量String usernameString password。之后调用 api 端点。

在 api 调用(上面的api.authenticate(username, password))之后,您必须以某种方式验证响应是否成功。这需要人为因素,具体取决于您的 API,// TODO 注释是您执行相同操作的提示。

如果 API 调用成功,您什么也不做,void 函数退出。否则,您的代码需要像 cmets 中所写的那样抛出 ApiException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2013-10-26
    • 2023-04-08
    相关资源
    最近更新 更多