I'm trying to adopt Swagger in REST API development (Spring Boot web application). API documenting process and code generation based on swagger spec works good, and now I've faced a problem writing integration tests using assertj-swagger and SpringFox libraries.
A few little words about these libraries. Springfox works by examining an application, once, at runtime to infer API semantics based on Spring configurations, class structure and various compile time java annotations. The swagger-assertj test library should compare a contract-first Swagger YAML file with a code-first Swagger JSON generated by SpringFox. For Consumer Driven Contract tests, assertj-swagger fails a test if it finds missing resources, methods, models, or properties in the implementation which are required by the consumer specification.
My test looks like (test code is taken from GitHub example):
@RunWith(SpringRunner.class)
@SpringBootTest
public class AssertJSwaggerConsumerDrivenTest {
@Test
public void validateThatImplementationSatisfiesConsumerSpecification() {
String designFirstSwagger = AssertJSwaggerConsumerDrivenTest.class.getResource("/swagger.yaml").getPath();
SwaggerAssertions.assertThat("http://localhost:8080/v2/api-docs")
.satisfiesContract(designFirstSwagger);
}
}
The problem is that this test is executed for a long time and seems to get stuck cause I don't see any log output after this line:
INFO c.s.e.AssertJSwaggerConsumerDrivenTest : Started AssertJSwaggerConsumerDrivenTest in 24.03 seconds (JVM running for 26.774)
GET http://localhost:8080/v2/api-docs
in my browser. Is there anybody experienced in using assertj-swagger cause it looks like I'm doing something wrong?
I've managed to run my AssertJSwaggerConsumerDrivenTest
! I think I've never been so happy to see a lot of red test results before =) Anyway it's better than running test forever.
AssertJ-Swagger's readme is a little bit outdated. Here's what I changed to fix the problem.
Test code:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AssertJSwaggerConsumerDrivenTest {
@LocalServerPort
int randomPort;
@Test
public void validateThatImplementationSatisfiesConsumerSpecification() {
File designFirstSwagger = new File(AssertJSwaggerConsumerDrivenTest.class.getResource("/swagger.yaml").getFile());
SwaggerAssertions.assertThat("http://localhost:" + randomPort + "/v2/api-docs")
.satisfiesContract(designFirstSwagger.getAbsolutePath());
}
}
pom.xml:
<dependency>
<groupId>io.github.robwin</groupId>
<artifactId>assertj-swagger</artifactId>
<version>0.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
<version>1.0.30</version>
<scope>test</scope>
</dependency>
Also I got java.lang.OutOfMemoryError: Permgen space
so I had to add this -XX:MaxPermSize=384m
to VM options.