2017-08-16 67 views
1

由codegen工具生成的Swagger API客户端仅创建返回类型为void的测试方法。我怎样才能测试我的休息API?Swagger API客户端方法返回void

这是我的服务代码规范:

@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 
+1

你可以发布你的规格?你生成了哪种语言/框架? – Helen

回答

1

JavaSwagger Codegen发电机尽其所​​能 - 它有带有函数的测试代码(更像函数存根),您可以在其中填写要传递给API端点的参数的值,例如上面的变量String usernameString password。之后,调用api端点。

在api调用(上面的api.authenticate(username, password))之后,您必须以某种方式验证响应是否成功。这需要一个取决于您的API的人为因素,并且// TODO评论意味着您也可以这样做。

如果API调用成功,则不执行任何操作,void函数退出。否则,你的代码需要抛出一个ApiException写在注释中。