Servlet JSON Response integration Tests

Avatar

Avatar

karthik4

Avatar

karthik4

karthik4

28-07-2020

Hello All,

There's requirement to expose a large sized JSON from an AEM servlet. The work is all complete. The Servlet is exposing the correct data. We are using unit tests to validate and ensure each and every property and type exists in the JSON response.

 

The next requirement is to write testes to validate against a swagger contract.

 

How can we set this up? Is this an integration test? How to get started?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

ChitraMadan

MVP

Avatar

ChitraMadan

MVP

ChitraMadan
MVP

28-07-2020

Hi @karthik4,

 

If you want to validate the swagger contract, you can try to validate the Yaml file generated by Swagger.

 

Please see below code for the same

 

import io.github.robwin.swagger.test.SwaggerAssertions;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.SwaggerParser;


private SwaggerParser swaggerParser = new SwaggerParser();

@Test
public void validateImplementationMatchesDocumentation() {
AuthorizationValue authorizationValue = new AuthorizationValue("Authorization", swaggerApiToken, "header");
Swagger remoteSwagger = swaggerParser.read(swaggerApiPath, Collections.singletonList(authorizationValue), false);
SwaggerAssertions.assertThat(remoteSwagger).isEqualTo("MOCKED VALUE");
}

You need to pass Swagger API Token and API Path like - 

https://api.swaggerhub.com/apis/organization/service-name/${swagger.api.version}/swagger.yaml

 

<dependency>
<groupId>io.github.robwin</groupId>
<artifactId>assertj-swagger</artifactId>
<version>${assertj-swagger.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.models.version}</version>
<scope>provided</scope>
</dependency>

 

Answers (0)