mercredi 16 octobre 2019

Spring Test api that is secured by Keycloak

I am developing a RESTful api with Spring and Keycloak(openid connect). The setup works in production. Now I want to write my Contract Tests. I've never done this before and I don't know how to enable to get a token during the test.

public class ServiceContractTest {

    private static RequestSpecification spec;

    @BeforeClass
    public static void initSpec(){
        spec = new RequestSpecBuilder()
                .setContentType(ContentType.JSON)
                .setBaseUri("http://localhost:8001/user")
                .addFilter(new ResponseLoggingFilter())
                .addFilter(new RequestLoggingFilter())
                .build();
    }
    @Test
    public void testCreateNewDevice() throws JSONException {

                 given()
                .spec(spec)
                .when()
                .post("/device")
                .then()
                .statusCode(201);

    }
}

This is the test I have written so far. I guess I have to do a @Before request to init the spec with a token. Can you tell me how to define the keycloak setting and the login parameters?

Aucun commentaire:

Enregistrer un commentaire