mirror of
https://gitee.com/dcren/initializr.git
synced 2026-09-19 22:37:40 +08:00
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:
@@ -53,6 +53,15 @@
|
|||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<profiles>
|
<profiles>
|
||||||
|
|||||||
@@ -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
|
raw Wiremock APIs, or via some features of
|
||||||
https://github.com/spring-cloud/spring-cloud-contract[Spring Cloud Contract].
|
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
|
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
|
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
|
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
|
== Using WireMock with Spring Boot
|
||||||
=== Loading Stubs from the Classpath
|
=== Loading Stubs from the Classpath
|
||||||
|
|
||||||
A convenient way to consume the stubs in your project is to add a test dependency:
|
A convenient way to consume the stubs in your project is to add a test dependency:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="attributes,specialchars"]
|
[source,xml,indent=0,subs="attributes,specialchars"]
|
||||||
----
|
----
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.spring.initializr</groupId>
|
<groupId>io.spring.initializr</groupId>
|
||||||
<artifactId>initializr-web</artifactId>
|
<artifactId>initializr-web</artifactId>
|
||||||
<classifier>stubs</classifier>
|
<classifier>stubs</classifier>
|
||||||
<version>{project-version}</version>
|
<version>{project-version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
----
|
----
|
||||||
|
|
||||||
and then pull the stubs from the classpath. In a Spring Boot application, using
|
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"]
|
[source,java,subs="attributes"]
|
||||||
----
|
----
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
|
||||||
@AutoConfigureWireMock(port = 0,
|
@AutoConfigureWireMock(port = 0,
|
||||||
stubs="classpath:META-INF/io.spring.initializr/initializr-web/{spring-initializr-version}")
|
stubs="classpath:META-INF/io.spring.initializr/initializr-web/{spring-initializr-version}")
|
||||||
public class ClientApplicationTests {
|
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>
|
||||||
<dependency>
|
<artifactId>spring-cloud-starter-contract-wiremock</artifactId>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<scope>test</scope>
|
||||||
<artifactId>spring-cloud-starter-contract-wiremock</artifactId>
|
</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>
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
TIP: This dependency is managed by the `spring-cloud-contract-dependencies` BOM.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
=== Using the Stub Runner
|
=== Using the Stub Runner
|
||||||
|
|
||||||
Alternatively you can configure the stub runner to look for the artifact, using a
|
Alternatively you can configure the stub runner to look for the artifact, using a
|
||||||
different Spring Cloud Contract dependency:
|
different Spring Cloud Contract dependency:
|
||||||
`spring-cloud-starter-contract-stub-runner`. The example below will automatically
|
`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):
|
stubs declared as a dependency):
|
||||||
|
|
||||||
[source,xml,indent=0,subs="attributes,specialchars"]
|
[source,xml,indent=0,subs="attributes,specialchars"]
|
||||||
@@ -93,40 +81,33 @@ stubs declared as a dependency):
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</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]
|
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
|
||||||
If you want to test a specific version or validate your API against multiple versions
|
of a custom client:
|
||||||
you can define the version to use in the annotation, something like
|
|
||||||
|
|
||||||
[source,java,indent=0,subs="attributes+"]
|
[source,java,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
@AutoConfigureStubRunner(
|
include::{test-examples}/stub/ClientApplicationTests.java[tag=test]
|
||||||
ids = "io.spring.initializr:initializr-web:{spring-initializr-version}",
|
|
||||||
workOffline = true)
|
|
||||||
public class InitializrIntegrationTests {
|
|
||||||
...
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
====
|
|
||||||
|
|
||||||
Then you have a server that returns the stub of the JSON metadata
|
Then you have a server that returns the stub of the JSON metadata
|
||||||
(`metadataWithCurrentAcceptHeader.json`) when you send it a header
|
(`metadataWithCurrentAcceptHeader.json`) when you send it a header
|
||||||
|
|||||||
+3
-3
@@ -23,13 +23,13 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.*;
|
import static org.springframework.boot.test.context.SpringBootTest.*;
|
||||||
|
|
||||||
// tag::test[]
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
|
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
|
||||||
@AutoConfigureStubRunner(
|
@AutoConfigureStubRunner(
|
||||||
ids = "io.spring.initializr:initializr-web",
|
ids = "io.spring.initializr:initializr-web:${project.version}",
|
||||||
workOffline = true)
|
workOffline = true)
|
||||||
public class InitializrIntegrationTests {
|
// tag::test[]
|
||||||
|
public class ClientApplicationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private StubFinder stubFinder;
|
private StubFinder stubFinder;
|
||||||
Reference in New Issue
Block a user