简体   繁体   中英

Swagger api client methods return void

Swagger api client, generated by codegen tool, only creates tests methods with return type void . How can I test my rest api ?

This is my service code specification:

 @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) 
{..}

And this is my generated swagger code for testing:

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

I have generate a swagger client api with:

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

The Java Generator for Swagger Codegen does the best that it can - it has test code with functions (more like function stubs) where you can fill in the value of the parameters to be passed to the API endpoints, such as the variables String username and String password above. After that, the api endpoint is called.

After the api call ( api.authenticate(username, password) above), you must somehow validate whether the response was successful. This needs a human element depending on your API, and the // TODO comment is a hint for you to do the same.

If the API is called successfully, you do nothing and the void function exits. Else, your code needs to throw a ApiException as written in the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM