From ffc691167f1ff62b4718d3f0e98d9b57fc3c5076 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 14 Nov 2016 10:57:59 +0100 Subject: [PATCH] Updated ports --- README.adoc | 37 ++++++++++++++++--- .../example/loan/LoanApplicationService.java | 2 +- ...oanApplicationServiceContextPathTests.java | 6 +-- .../loan/LoanApplicationServiceTests.java | 6 +-- .../test/resources/application-gradle.yaml | 2 +- .../src/test/resources/application.yaml | 5 +-- .../src/main/resources/application.yml | 2 +- .../src/test/resources/application.yml | 3 +- .../src/main/resources/application.properties | 1 + .../src/main/resources/application.yml | 2 +- 10 files changed, 47 insertions(+), 19 deletions(-) diff --git a/README.adoc b/README.adoc index 972a3cf3ad..696fa0f4b9 100644 --- a/README.adoc +++ b/README.adoc @@ -56,10 +56,12 @@ To start the stub server on a different port use `@AutoConfigureWireMock(port=99 === Registering Stubs Automatically -If you add a `stubs` attribute to your `@AutoConfigureWireMock` then -it will register WireMock JSON stubs from the file system or -classpath. The stubs attribute can be a resource pattern (ant-style) -or a directory, in which case `**/*.json` is appended. Example: +If you use `@AutoConfigureWireMock` then it will register WireMock +JSON stubs from the file system or classpath, by default from +`file:src/test/resources/mappings`. You can customize the locations +using the `stubs` attribute in the annotation, which can be a resource +pattern (ant-style) or a directory, in which case `**/*.json` is +appended. Example: ---- @RunWith(SpringRunner.class) @@ -78,6 +80,30 @@ public class WiremockImportApplicationTests { } ---- +NOTE: Actually WireMock always loads mappings from +`src/test/resources/mappings` *as well as* the custom locations in the +stubs attribute. To change this behaviour you have to also specify a +files root as described next. + +=== Using Files to Specify the Stub Bodies + +WireMock can read response bodies from files on the classpath or file +system. In that case you will see in the JSON DSL that the response +has a "bodyFileName" instead of a (literal) "body". The files are +resolved relative to a root directory `src/test/resources/__files` by +default. To customize this location you can set the `files` attribute +in the `@AutoConfigureWireMock` annotation to the location of the +parent directory (i.e. the place where `__files` is a +subdirectory). You can use Spring resource notation to refer to +`file:...` or `classpath:...` locations (but generic URLs are not +supported). A list of values can be given and WireMock will resolve +the first file that exists when it needs to find a response body. + +NOTE: when you configure the `files` root, then it affects the +automatic loading of stubs as well (they come from the root location +in a subdirectory called "mappings"). The value of `files` has no +effect on the stubs loaded explicitly from the `stubs` attribute. + === Alternative: Using JUnit Rules For a more conventional WireMock experience, using JUnit `@Rules` to @@ -760,8 +786,9 @@ Annotate your test class with `@AutoConfigureStubRunner`. In the annotation prov [source,groovy,indent=0] ---- @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(webEnvironment=WebEnvironment.NONE) @AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:8080"}, workOffline = true) +@DirtiesContext public class LoanApplicationServiceTests { ---- diff --git a/samples/standalone/dsl/http-client/src/main/java/com/example/loan/LoanApplicationService.java b/samples/standalone/dsl/http-client/src/main/java/com/example/loan/LoanApplicationService.java index 33cd026aac..8b38706c63 100644 --- a/samples/standalone/dsl/http-client/src/main/java/com/example/loan/LoanApplicationService.java +++ b/samples/standalone/dsl/http-client/src/main/java/com/example/loan/LoanApplicationService.java @@ -22,7 +22,7 @@ public class LoanApplicationService { private final RestTemplate restTemplate; - private int port = 8080; + private int port = 6565; public LoanApplicationService() { this.restTemplate = new RestTemplate(); diff --git a/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceContextPathTests.java b/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceContextPathTests.java index 4b9653244a..09be30344a 100644 --- a/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceContextPathTests.java +++ b/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceContextPathTests.java @@ -1,5 +1,7 @@ package com.example.loan; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -14,12 +16,10 @@ import com.example.loan.model.LoanApplication; import com.example.loan.model.LoanApplicationResult; import com.example.loan.model.LoanApplicationStatus; -import static org.assertj.core.api.Assertions.assertThat; - // tag::autoconfigure_stubrunner[] @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.NONE, properties="server.context-path=/app") -@AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:8080"}, workOffline = true) +@AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:6565"}, workOffline = true) @DirtiesContext public class LoanApplicationServiceContextPathTests { // end::autoconfigure_stubrunner[] diff --git a/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceTests.java b/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceTests.java index 07360760b7..f5755c22bd 100644 --- a/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceTests.java +++ b/samples/standalone/dsl/http-client/src/test/java/com/example/loan/LoanApplicationServiceTests.java @@ -1,5 +1,7 @@ package com.example.loan; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -14,12 +16,10 @@ import com.example.loan.model.LoanApplication; import com.example.loan.model.LoanApplicationResult; import com.example.loan.model.LoanApplicationStatus; -import static org.assertj.core.api.Assertions.assertThat; - // tag::autoconfigure_stubrunner[] @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.NONE) -@AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:8080"}, workOffline = true) +@AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:6565"}, workOffline = true) @DirtiesContext public class LoanApplicationServiceTests { // end::autoconfigure_stubrunner[] diff --git a/samples/standalone/dsl/http-client/src/test/resources/application-gradle.yaml b/samples/standalone/dsl/http-client/src/test/resources/application-gradle.yaml index a029079b57..3ea79d4f5f 100644 --- a/samples/standalone/dsl/http-client/src/test/resources/application-gradle.yaml +++ b/samples/standalone/dsl/http-client/src/test/resources/application-gradle.yaml @@ -1,3 +1,3 @@ stubrunner: work-offline: true - stubs.ids: 'com.example:http-server-dsl-gradle:+:stubs:8080' \ No newline at end of file + stubs.ids: 'com.example:http-server-dsl-gradle:+:stubs:6565' \ No newline at end of file diff --git a/samples/standalone/dsl/http-client/src/test/resources/application.yaml b/samples/standalone/dsl/http-client/src/test/resources/application.yaml index 0d73045e2e..698f3a3d73 100644 --- a/samples/standalone/dsl/http-client/src/test/resources/application.yaml +++ b/samples/standalone/dsl/http-client/src/test/resources/application.yaml @@ -1,3 +1,2 @@ -stubrunner: - work-offline: true - ids: 'com.example:http-server-dsl:+:stubs:8080' \ No newline at end of file +server: + port: 6565 \ No newline at end of file diff --git a/samples/standalone/dsl/http-server/src/main/resources/application.yml b/samples/standalone/dsl/http-server/src/main/resources/application.yml index a30a91f034..1c421cf2b7 100644 --- a/samples/standalone/dsl/http-server/src/main/resources/application.yml +++ b/samples/standalone/dsl/http-server/src/main/resources/application.yml @@ -1 +1 @@ -server.port=8085 \ No newline at end of file +server.port=0 \ No newline at end of file diff --git a/samples/standalone/messaging/stream-sink/src/test/resources/application.yml b/samples/standalone/messaging/stream-sink/src/test/resources/application.yml index 2b8a53b307..8082d660f8 100644 --- a/samples/standalone/messaging/stream-sink/src/test/resources/application.yml +++ b/samples/standalone/messaging/stream-sink/src/test/resources/application.yml @@ -1,3 +1,4 @@ stubrunner: work-offline: true - ids: 'com.example:stream-source' \ No newline at end of file + ids: 'com.example:stream-source' +server.port: 0 \ No newline at end of file diff --git a/samples/standalone/messaging/stream-source/src/main/resources/application.properties b/samples/standalone/messaging/stream-source/src/main/resources/application.properties index 33a33fc971..990afb954e 100644 --- a/samples/standalone/messaging/stream-source/src/main/resources/application.properties +++ b/samples/standalone/messaging/stream-source/src/main/resources/application.properties @@ -1 +1,2 @@ spring.cloud.stream.bindings.output.contentType=application/json +server.port=0 \ No newline at end of file diff --git a/samples/standalone/restdocs/http-server/src/main/resources/application.yml b/samples/standalone/restdocs/http-server/src/main/resources/application.yml index a30a91f034..1c421cf2b7 100644 --- a/samples/standalone/restdocs/http-server/src/main/resources/application.yml +++ b/samples/standalone/restdocs/http-server/src/main/resources/application.yml @@ -1 +1 @@ -server.port=8085 \ No newline at end of file +server.port=0 \ No newline at end of file