API testing by passing token

Asked

Viewed 140 times

0

Good morning,

I’m doing Api Testing, as follows the example:

@Test
public void shouldStatus200_FindAll() {
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

    given().basePath("/receitas")
           .port(port)
           .accept(ContentType.JSON)
    .when().get()
    .then().statusCode(HttpStatus.OK.value());
}

As in the system has implemented Token, the test shows an authentication error, follows the error:

{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}

How do I pass authentication in the test, to check correctly?

1 answer

1

I managed to solve, follows in example:

@Test
public void shouldStatus200_FindAll() {
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

    given().auth().oauth2("String token")
           .basePath("/receitas")
           .port(port)
           .accept(ContentType.JSON)
    .when().get()
    .then().statusCode(HttpStatus.OK.value());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.