From f30d48e3b3e279f2c405e2e28ce5c27710a5942c Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 23 Apr 2021 23:41:06 -0700 Subject: [PATCH] Extract code samples from docs See gh-6313 --- .../spring-boot-docs/build.gradle | 2 + .../docs/asciidoc/spring-boot-features.adoc | 178 ++++-------------- .../springbootfeatures/jta/nonxa/MyBean.java | 31 +++ .../jta/primary/MyBean.java | 29 +++ .../springbootfeatures/jta/xa/MyBean.java | 31 +++ .../springbootfeatures/quartz/SampleJob.java | 58 ++++++ .../testing/MockWebTestClientTests.java | 8 +- .../testing/RandomPortWebTestClientTests.java | 8 +- .../detection/MyWebFluxTests.java | 26 +++ .../applications/excluding/MyTests.java | 38 ++++ .../testing/applications/json/AssertJ.java | 50 +++++ .../applications/json/MyJsonTests.java | 72 +++++++ .../testing/applications/mocking/MyTests.java | 61 ++++++ .../mocking/listener/MyTests.java | 35 ++++ .../springbootfeatures/validation/MyBean.java | 41 ++++ .../webclient/MyService.java | 38 ++++ 16 files changed, 559 insertions(+), 147 deletions(-) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/nonxa/MyBean.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/primary/MyBean.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/xa/MyBean.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/quartz/SampleJob.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/detection/MyWebFluxTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/excluding/MyTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/AssertJ.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/MyJsonTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/MyTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/listener/MyTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/validation/MyBean.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webclient/MyService.java diff --git a/spring-boot-project/spring-boot-docs/build.gradle b/spring-boot-project/spring-boot-docs/build.gradle index 8d8db4a12f4..6b50f22d94f 100644 --- a/spring-boot-project/spring-boot-docs/build.gradle +++ b/spring-boot-project/spring-boot-docs/build.gradle @@ -83,7 +83,9 @@ dependencies { implementation("org.glassfish.jersey.core:jersey-server") implementation("org.hibernate:hibernate-jcache") implementation("org.jooq:jooq") + implementation("org.mockito:mockito-core") implementation("org.mongodb:mongodb-driver-sync") + implementation("org.quartz-scheduler:quartz") implementation("org.slf4j:jul-to-slf4j") implementation("org.springframework:spring-jdbc") implementation("org.springframework:spring-jms") diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index fb4518b3240..ba7e79f68cc 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -5515,23 +5515,9 @@ Spring Boot is configuring that builder to share HTTP resources, reflect codecs The following code shows a typical example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Service - public class MyService { - - private final WebClient webClient; - - public MyService(WebClient.Builder webClientBuilder) { - this.webClient = webClientBuilder.baseUrl("https://example.org").build(); - } - - public Mono
someRestCall(String name) { - return this.webClient.get().uri("/{name}/details", name) - .retrieve().bodyToMono(Details.class); - } - - } +include::{include-springbootfeatures}/webclient/MyService.java[] ---- @@ -5576,18 +5562,9 @@ Target classes with such annotated methods need to be annotated with the `@Valid For instance, the following service triggers the validation of the first argument, making sure its size is between 8 and 10: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Service - @Validated - public class MyBean { - - public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code, - Author author) { - ... - } - - } +include::{include-springbootfeatures}/validation/MyBean.java[] ---- @@ -5668,29 +5645,28 @@ Spring Boot tries to auto-configure JMS by looking for a `ConnectionFactory` at [[boot-features-jta-mixed-jms]] === Mixing XA and Non-XA JMS Connections When using JTA, the primary JMS `ConnectionFactory` bean is XA-aware and participates in distributed transactions. +You can inject into your bean without needing to use any `@Qualifier`: + +[source,java,indent=0] +---- +include::{include-springbootfeatures}/jta/primary/MyBean.java[tags=*] +---- + In some situations, you might want to process certain JMS messages by using a non-XA `ConnectionFactory`. For example, your JMS processing logic might take longer than the XA timeout. -If you want to use a non-XA `ConnectionFactory`, you can inject the `nonXaJmsConnectionFactory` bean rather than the `@Primary` `jmsConnectionFactory` bean. -For consistency, the `jmsConnectionFactory` bean is also provided by using the bean alias `xaJmsConnectionFactory`. +If you want to use a non-XA `ConnectionFactory`, you can the `nonXaJmsConnectionFactory` bean: -The following example shows how to inject `ConnectionFactory` instances: - -[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] +[source,java,indent=0] +---- +include::{include-springbootfeatures}/jta/nonxa/MyBean.java[tags=*] ---- - // Inject the primary (XA aware) ConnectionFactory - @Autowired - private ConnectionFactory defaultConnectionFactory; - // Inject the XA aware ConnectionFactory (uses the alias and injects the same as above) - @Autowired - @Qualifier("xaJmsConnectionFactory") - private ConnectionFactory xaConnectionFactory; +For consistency, the `jmsConnectionFactory` bean is also provided by using the bean alias `xaJmsConnectionFactory`: - // Inject the non-XA aware ConnectionFactory - @Autowired - @Qualifier("nonXaJmsConnectionFactory") - private ConnectionFactory nonXaConnectionFactory; +[source,java,indent=0] +---- +include::{include-springbootfeatures}/jta/xa/MyBean.java[tags=*] ---- @@ -5794,27 +5770,9 @@ If you need to customize the task executor, consider implementing `SchedulerFact Jobs can define setters to inject data map properties. Regular beans can also be injected in a similar manner, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - public class SampleJob extends QuartzJobBean { - - private MyService myService; - - private String name; - - // Inject "MyService" bean - public void setMyService(MyService myService) { ... } - - // Inject the "name" job data property - public void setName(String name) { ... } - - @Override - protected void executeInternal(JobExecutionContext context) - throws JobExecutionException { - ... - } - - } +include::{include-springbootfeatures}/quartz/SampleJob.java[] ---- @@ -6090,10 +6048,9 @@ If you have only Spring WebFlux, we'll detect that and configure a WebFlux-based If both are present, Spring MVC takes precedence. If you want to test a reactive web application in this scenario, you must set the configprop:spring.main.web-application-type[] property: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @SpringBootTest(properties = "spring.main.web-application-type=reactive") - class MyWebFluxTests { ... } +include::{include-springbootfeatures}/testing/applications/detection/MyWebFluxTests.java[] ---- @@ -6134,18 +6091,9 @@ As we < json; - - @Test - void testSerialize() throws Exception { - VehicleDetails details = new VehicleDetails("Honda", "Civic"); - // Assert against a `.json` file in the same package as the test - assertThat(this.json.write(details)).isEqualToJson("expected.json"); - // Or use JSON path based assertions - assertThat(this.json.write(details)).hasJsonPathStringValue("@.make"); - assertThat(this.json.write(details)).extractingJsonPathStringValue("@.make") - .isEqualTo("Honda"); - } - - @Test - void testDeserialize() throws Exception { - String content = "{\"make\":\"Ford\",\"model\":\"Focus\"}"; - assertThat(this.json.parse(content)) - .isEqualTo(new VehicleDetails("Ford", "Focus")); - assertThat(this.json.parseObject(content).getMake()).isEqualTo("Ford"); - } - - } +include::{include-springbootfeatures}/testing/applications/json/MyJsonTests.java[] ---- NOTE: JSON helper classes can also be used directly in standard unit tests. @@ -6409,11 +6299,9 @@ If you're using Spring Boot's AssertJ-based helpers to assert on a number value Instead, you can use AssertJ's `satisfies` to assert that the value matches the given condition. For instance, the following example asserts that the actual number is a float value close to `0.15` within an offset of `0.01`. -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - assertThat(json.write(message)) - .extractingJsonPathNumberValue("@.test.numberValue") - .satisfies((number) -> assertThat(number.floatValue()).isCloseTo(0.15f, within(0.01f))); +include::{include-springbootfeatures}/testing/applications/json/AssertJ.java[tags=*] ---- diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/nonxa/MyBean.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/nonxa/MyBean.java new file mode 100644 index 00000000000..594dca98222 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/nonxa/MyBean.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.jta.nonxa; + +import javax.jms.ConnectionFactory; + +import org.springframework.beans.factory.annotation.Qualifier; + +public class MyBean { + + // tag::code[] + public MyBean(@Qualifier("nonXaJmsConnectionFactory") ConnectionFactory connectionFactory) { + // ... + } + // end::code[] + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/primary/MyBean.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/primary/MyBean.java new file mode 100644 index 00000000000..ef4e2423cf1 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/primary/MyBean.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.jta.primary; + +import javax.jms.ConnectionFactory; + +public class MyBean { + + // tag::code[] + public MyBean(ConnectionFactory connectionFactory) { + // ... + } + // end::code[] + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/xa/MyBean.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/xa/MyBean.java new file mode 100644 index 00000000000..a5fa80ebdbd --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/jta/xa/MyBean.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.jta.xa; + +import javax.jms.ConnectionFactory; + +import org.springframework.beans.factory.annotation.Qualifier; + +public class MyBean { + + // tag::code[] + public MyBean(@Qualifier("xaJmsConnectionFactory") ConnectionFactory connectionFactory) { + // ... + } + // end::code[] + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/quartz/SampleJob.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/quartz/SampleJob.java new file mode 100644 index 00000000000..62b183e7d27 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/quartz/SampleJob.java @@ -0,0 +1,58 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.quartz; + +import java.util.Date; + +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; + +import org.springframework.scheduling.quartz.QuartzJobBean; + +public class SampleJob extends QuartzJobBean { + + // @fold:on // fields ... + private MyService myService; + + private String name; + + // @fold:off + + // Inject "MyService" bean + public void setMyService(MyService myService) { + this.myService = myService; + } + + // Inject the "name" job data property + public void setName(String name) { + this.name = name; + } + + @Override + protected void executeInternal(JobExecutionContext context) throws JobExecutionException { + this.myService.someMethod(context.getFireTime(), this.name); + } + +} +// @chomp:file + +class MyService { + + void someMethod(Date date, String name) { + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/MockWebTestClientTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/MockWebTestClientTests.java index 1b17c1bc342..736cba508e3 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/MockWebTestClientTests.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/MockWebTestClientTests.java @@ -29,7 +29,13 @@ class MockWebTestClientTests { @Test void exampleTest(@Autowired WebTestClient webClient) { - webClient.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello World"); + // @formatter:off + webClient + .get().uri("/") + .exchange() + .expectStatus().isOk() + .expectBody(String.class).isEqualTo("Hello World"); + // @formatter:on } } diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/RandomPortWebTestClientTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/RandomPortWebTestClientTests.java index c5c5257374a..5581139a852 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/RandomPortWebTestClientTests.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/RandomPortWebTestClientTests.java @@ -28,7 +28,13 @@ class RandomPortWebTestClientTests { @Test void exampleTest(@Autowired WebTestClient webClient) { - webClient.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello World"); + // @formatter:off + webClient + .get().uri("/") + .exchange() + .expectStatus().isOk() + .expectBody(String.class).isEqualTo("Hello World"); + // @formatter:on } } diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/detection/MyWebFluxTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/detection/MyWebFluxTests.java new file mode 100644 index 00000000000..57e66b2abcb --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/detection/MyWebFluxTests.java @@ -0,0 +1,26 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.testing.applications.detection; + +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(properties = "spring.main.web-application-type=reactive") +class MyWebFluxTests { + + // ... + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/excluding/MyTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/excluding/MyTests.java new file mode 100644 index 00000000000..0bd543ba9fb --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/excluding/MyTests.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.testing.applications.excluding; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; + +@SpringBootTest +@Import(MyTestsConfiguration.class) +class MyTests { + + @Test + void exampleTest() { + // ... + } + +} +// @chomp:file + +class MyTestsConfiguration { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/AssertJ.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/AssertJ.java new file mode 100644 index 00000000000..4dff95a73f2 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/AssertJ.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.testing.applications.json; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; + +@JsonTest +class AssertJ { + + @Autowired + private JacksonTester json; + + // tag::code[] + @Test + void someTest() throws Exception { + SomeObject value = new SomeObject(0.152f); + assertThat(this.json.write(value)).extractingJsonPathNumberValue("@.test.numberValue") + .satisfies((number) -> assertThat(number.floatValue()).isCloseTo(0.15f, within(0.01f))); + } + // end::code[] + +} + +class SomeObject { + + SomeObject(float value) { + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/MyJsonTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/MyJsonTests.java new file mode 100644 index 00000000000..c97b8056273 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/json/MyJsonTests.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.testing.applications.json; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; + +import static org.assertj.core.api.Assertions.assertThat; + +@JsonTest +class MyJsonTests { + + @Autowired + private JacksonTester json; + + @Test + void serialize() throws Exception { + VehicleDetails details = new VehicleDetails("Honda", "Civic"); + // Assert against a `.json` file in the same package as the test + assertThat(this.json.write(details)).isEqualToJson("expected.json"); + // Or use JSON path based assertions + assertThat(this.json.write(details)).hasJsonPathStringValue("@.make"); + assertThat(this.json.write(details)).extractingJsonPathStringValue("@.make").isEqualTo("Honda"); + } + + @Test + void deserialize() throws Exception { + String content = "{\"make\":\"Ford\",\"model\":\"Focus\"}"; + assertThat(this.json.parse(content)).isEqualTo(new VehicleDetails("Ford", "Focus")); + assertThat(this.json.parseObject(content).getMake()).isEqualTo("Ford"); + } + +} +// @chomp:file + +class VehicleDetails { + + private final String make; + + private final String model; + + VehicleDetails(String make, String model) { + this.make = make; + this.model = model; + } + + String getMake() { + return this.make; + } + + String getModel() { + return this.model; + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/MyTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/MyTests.java new file mode 100644 index 00000000000..bb2e9d2805e --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/MyTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.testing.applications.mocking; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; + +@SpringBootTest +class MyTests { + + @Autowired + private Reverser reverser; + + @MockBean + private RemoteService remoteService; + + @Test + void exampleTest() { + given(this.remoteService.getValue()).willReturn("spring"); + String reverse = this.reverser.getReverseValue(); // Calls injected RemoteService + assertThat(reverse).isEqualTo("gnirps"); + } + +} +// @chomp:file + +class RemoteService { + + Object getValue() { + return null; + } + +} + +class Reverser { + + String getReverseValue() { + return null; + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/listener/MyTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/listener/MyTests.java new file mode 100644 index 00000000000..74890be2a30 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/mocking/listener/MyTests.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.testing.applications.mocking.listener; + +import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener; +import org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; + +@ContextConfiguration(classes = MyConfig.class) +@TestExecutionListeners({ MockitoTestExecutionListener.class, ResetMocksTestExecutionListener.class }) +public class MyTests { + + // ... + +} +// @chomp:file + +class MyConfig { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/validation/MyBean.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/validation/MyBean.java new file mode 100644 index 00000000000..100b2713c1c --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/validation/MyBean.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.validation; + +import javax.validation.constraints.Size; + +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +@Service +@Validated +public class MyBean { + + public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code, Author author) { + return /**/ null; + } + +} +// @chomp:file + +class Archive { + +} + +class Author { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webclient/MyService.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webclient/MyService.java new file mode 100644 index 00000000000..9b4c7625fb4 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webclient/MyService.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.webclient; + +import org.neo4j.cypherdsl.core.Relationship.Details; +import reactor.core.publisher.Mono; + +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; + +@Service +public class MyService { + + private final WebClient webClient; + + public MyService(WebClient.Builder webClientBuilder) { + this.webClient = webClientBuilder.baseUrl("https://example.org").build(); + } + + public Mono
someRestCall(String name) { + return this.webClient.get().uri("/{name}/details", name).retrieve().bodyToMono(Details.class); + } + +}