Swagger api客户端方法返回void

时间:2017-08-16 09:14:13

标签: java api testing swagger codegen

由codegen工具生成的Swagger api客户端仅创建返回类型为 void 的测试方法。我怎样才能测试我的休息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) 
{..}

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

/**
  * 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
}}

我已经生成了一个swagger客户端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

1 个答案:

答案 0 :(得分:1)

Java的{​​{1}}生成器尽其所能 - 它具有带函数的测试代码(更像是函数存根),您可以在其中填写要传递给的函数的值。 API端点,例如上面的变量Swagger CodegenString username。之后,调用api端点。

在api调用(上面String password)之后,您必须以某种方式验证响应是否成功。这需要一个人类元素,具体取决于您的API,api.authenticate(username, password)评论是您提供相同内容的提示。

如果成功调用API,则不执行任何操作,并退出void函数。否则,您的代码需要抛出注释中所写的// TODO