Fix spring cloud contract test to use a defined stub version

Rather than attempting to retrieve the latest version of the stubs, this
commit makes sure the test uses the same version as the one of the
project.

This fixes gh-422 in a different way
This commit is contained in:
Stephane Nicoll
2017-05-08 12:42:32 +02:00
parent f2089800b2
commit 66dad39188
3 changed files with 51 additions and 61 deletions

View File

@@ -53,6 +53,15 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<project.version>${project.version}</project.version>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>

View File

@@ -6,7 +6,7 @@ service, you can use these stubs to test your own code. You can consume them wit
raw Wiremock APIs, or via some features of
https://github.com/spring-cloud/spring-cloud-contract[Spring Cloud Contract].
WireMock is an embedded web server that analyses incoming requests and chooses stub
TIP: WireMock is an embedded web server that analyses incoming requests and chooses stub
responses based on matching some rules (e.g. a specific header value). So if you send
it a request which matches one of its stubs, it will send you a response as if it was
a real Initializr service, and you can use that to do full stack integration testing
@@ -16,18 +16,17 @@ of your client.
== Using WireMock with Spring Boot
=== Loading Stubs from the Classpath
A convenient way to consume the stubs in your project is to add a test dependency:
[source,xml,indent=0,subs="attributes,specialchars"]
----
<dependency>
<groupId>io.spring.initializr</groupId>
<artifactId>initializr-web</artifactId>
<classifier>stubs</classifier>
<version>{project-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.spring.initializr</groupId>
<artifactId>initializr-web</artifactId>
<classifier>stubs</classifier>
<version>{project-version}</version>
<scope>test</scope>
</dependency>
----
and then pull the stubs from the classpath. In a Spring Boot application, using
@@ -37,7 +36,7 @@ with it like this:
[source,java,subs="attributes"]
----
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@AutoConfigureWireMock(port = 0,
stubs="classpath:META-INF/io.spring.initializr/initializr-web/{spring-initializr-version}")
public class ClientApplicationTests {
@@ -50,37 +49,26 @@ public class ClientApplicationTests {
}
----
The wiremock fetaures come with Spring Cloud Contract Wiremock:
The Wiremock features come with Spring Cloud Contract Wiremock:
[source,xml,indent=0,subs="attributes,specialchars"]
[source,xml,indent=0]
----
<dependencies>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-wiremock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-dependencies</artifactId>
<version>1.0.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-wiremock</artifactId>
<scope>test</scope>
</dependency>
----
TIP: This dependency is managed by the `spring-cloud-contract-dependencies` BOM.
=== Using the Stub Runner
Alternatively you can configure the stub runner to look for the artifact, using a
different Spring Cloud Contract dependency:
`spring-cloud-starter-contract-stub-runner`. The example below will automatically
download, if necessary, the latest version of the initializr stubs (so you don't need the
download, if necessary, the defined version of the initializr stubs (so you don't need the
stubs declared as a dependency):
[source,xml,indent=0,subs="attributes,specialchars"]
@@ -93,40 +81,33 @@ stubs declared as a dependency):
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-dependencies</artifactId>
<version>1.0.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
----
The test should use `@AutoConfigureStubRunner` instead:
[source,java,indent=0]
[source,java,subs="attributes,specialchars"]
----
include::{test-examples}/stub/InitializrIntegrationTests.java[tag=test]
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = "io.spring.initializr:initializr-web:{spring-initializr-version}")
public class ClientApplicationTests {
@Value("${wiremock.server.port}")
private int port;
...
}
----
[TIP]
====
If you want to test a specific version or validate your API against multiple versions
you can define the version to use in the annotation, something like
Here is an example of a test that retrieves the metadata of the service. The assertions
do not matter much here but it illustrates how you could integrate that in the test suite
of a custom client:
[source,java,indent=0,subs="attributes+"]
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@AutoConfigureStubRunner(
ids = "io.spring.initializr:initializr-web:{spring-initializr-version}",
workOffline = true)
public class InitializrIntegrationTests {
...
}
include::{test-examples}/stub/ClientApplicationTests.java[tag=test]
----
====
Then you have a server that returns the stub of the JSON metadata
(`metadataWithCurrentAcceptHeader.json`) when you send it a header

View File

@@ -23,13 +23,13 @@ import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.boot.test.context.SpringBootTest.*;
// tag::test[]
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@AutoConfigureStubRunner(
ids = "io.spring.initializr:initializr-web",
ids = "io.spring.initializr:initializr-web:${project.version}",
workOffline = true)
public class InitializrIntegrationTests {
// tag::test[]
public class ClientApplicationTests {
@Autowired
private StubFinder stubFinder;