mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Extract code samples from docs
See gh-6313
This commit is contained in:
parent
02cc778911
commit
f30d48e3b3
@ -83,7 +83,9 @@ dependencies {
|
|||||||
implementation("org.glassfish.jersey.core:jersey-server")
|
implementation("org.glassfish.jersey.core:jersey-server")
|
||||||
implementation("org.hibernate:hibernate-jcache")
|
implementation("org.hibernate:hibernate-jcache")
|
||||||
implementation("org.jooq:jooq")
|
implementation("org.jooq:jooq")
|
||||||
|
implementation("org.mockito:mockito-core")
|
||||||
implementation("org.mongodb:mongodb-driver-sync")
|
implementation("org.mongodb:mongodb-driver-sync")
|
||||||
|
implementation("org.quartz-scheduler:quartz")
|
||||||
implementation("org.slf4j:jul-to-slf4j")
|
implementation("org.slf4j:jul-to-slf4j")
|
||||||
implementation("org.springframework:spring-jdbc")
|
implementation("org.springframework:spring-jdbc")
|
||||||
implementation("org.springframework:spring-jms")
|
implementation("org.springframework:spring-jms")
|
||||||
|
@ -5515,23 +5515,9 @@ Spring Boot is configuring that builder to share HTTP resources, reflect codecs
|
|||||||
|
|
||||||
The following code shows a typical example:
|
The following code shows a typical example:
|
||||||
|
|
||||||
[source,java,pending-extract=true,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
@Service
|
include::{include-springbootfeatures}/webclient/MyService.java[]
|
||||||
public class MyService {
|
|
||||||
|
|
||||||
private final WebClient webClient;
|
|
||||||
|
|
||||||
public MyService(WebClient.Builder webClientBuilder) {
|
|
||||||
this.webClient = webClientBuilder.baseUrl("https://example.org").build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Mono<Details> someRestCall(String name) {
|
|
||||||
return this.webClient.get().uri("/{name}/details", name)
|
|
||||||
.retrieve().bodyToMono(Details.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
@ -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:
|
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
|
include::{include-springbootfeatures}/validation/MyBean.java[]
|
||||||
@Validated
|
|
||||||
public class MyBean {
|
|
||||||
|
|
||||||
public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code,
|
|
||||||
Author author) {
|
|
||||||
...
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
@ -5668,29 +5645,28 @@ Spring Boot tries to auto-configure JMS by looking for a `ConnectionFactory` at
|
|||||||
[[boot-features-jta-mixed-jms]]
|
[[boot-features-jta-mixed-jms]]
|
||||||
=== Mixing XA and Non-XA JMS Connections
|
=== Mixing XA and Non-XA JMS Connections
|
||||||
When using JTA, the primary JMS `ConnectionFactory` bean is XA-aware and participates in distributed transactions.
|
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`.
|
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.
|
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.
|
If you want to use a non-XA `ConnectionFactory`, you can the `nonXaJmsConnectionFactory` bean:
|
||||||
For consistency, the `jmsConnectionFactory` bean is also provided by using the bean alias `xaJmsConnectionFactory`.
|
|
||||||
|
|
||||||
The following example shows how to inject `ConnectionFactory` instances:
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"]
|
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)
|
For consistency, the `jmsConnectionFactory` bean is also provided by using the bean alias `xaJmsConnectionFactory`:
|
||||||
@Autowired
|
|
||||||
@Qualifier("xaJmsConnectionFactory")
|
|
||||||
private ConnectionFactory xaConnectionFactory;
|
|
||||||
|
|
||||||
// Inject the non-XA aware ConnectionFactory
|
[source,java,indent=0]
|
||||||
@Autowired
|
----
|
||||||
@Qualifier("nonXaJmsConnectionFactory")
|
include::{include-springbootfeatures}/jta/xa/MyBean.java[tags=*]
|
||||||
private ConnectionFactory nonXaConnectionFactory;
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
@ -5794,27 +5770,9 @@ If you need to customize the task executor, consider implementing `SchedulerFact
|
|||||||
Jobs can define setters to inject data map properties.
|
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:
|
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 {
|
include::{include-springbootfeatures}/quartz/SampleJob.java[]
|
||||||
|
|
||||||
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 {
|
|
||||||
...
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
@ -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 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:
|
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")
|
include::{include-springbootfeatures}/testing/applications/detection/MyWebFluxTests.java[]
|
||||||
class MyWebFluxTests { ... }
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
@ -6134,18 +6091,9 @@ As we <<boot-features-testing-spring-boot-applications-detecting-config,have see
|
|||||||
When placed on a top-level class, `@TestConfiguration` indicates that classes in `src/test/java` should not be picked up by scanning.
|
When placed on a top-level class, `@TestConfiguration` indicates that classes in `src/test/java` should not be picked up by scanning.
|
||||||
You can then import that class explicitly where it is required, as shown in the following example:
|
You can then import that class explicitly where it is required, as shown in the following example:
|
||||||
|
|
||||||
[source,java,pending-extract=true,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
@SpringBootTest
|
include::{include-springbootfeatures}/testing/applications/excluding/MyTests.java[]
|
||||||
@Import(MyTestsConfiguration.class)
|
|
||||||
class MyTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void exampleTest() {
|
|
||||||
...
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
NOTE: If you directly use `@ComponentScan` (that is, not through `@SpringBootApplication`) you need to register the `TypeExcludeFilter` with it.
|
NOTE: If you directly use `@ComponentScan` (that is, not through `@SpringBootApplication`) you need to register the `TypeExcludeFilter` with it.
|
||||||
@ -6264,43 +6212,18 @@ Mock beans are automatically reset after each test method.
|
|||||||
If your test uses one of Spring Boot's test annotations (such as `@SpringBootTest`), this feature is automatically enabled.
|
If your test uses one of Spring Boot's test annotations (such as `@SpringBootTest`), this feature is automatically enabled.
|
||||||
To use this feature with a different arrangement, listeners must be explicitly added, as shown in the following example:
|
To use this feature with a different arrangement, listeners must be explicitly added, as shown in the following example:
|
||||||
|
|
||||||
[source,java,pending-extract=true,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
@TestExecutionListeners({ MockitoTestExecutionListener.class, ResetMocksTestExecutionListener.class })
|
include::{include-springbootfeatures}/testing/applications/mocking/listener/MyTests.java[]
|
||||||
----
|
----
|
||||||
|
|
||||||
====
|
====
|
||||||
|
|
||||||
The following example replaces an existing `RemoteService` bean with a mock implementation:
|
The following example replaces an existing `RemoteService` bean with a mock implementation:
|
||||||
|
|
||||||
[source,java,pending-extract=true,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
import org.junit.jupiter.api.Test;
|
include::{include-springbootfeatures}/testing/applications/mocking/MyTests.java[]
|
||||||
import org.springframework.beans.factory.annotation.*;
|
|
||||||
import org.springframework.boot.test.context.*;
|
|
||||||
import org.springframework.boot.test.mock.mockito.*;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
|
||||||
import static org.mockito.BDDMockito.*;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
class MyTests {
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private RemoteService remoteService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Reverser reverser;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void exampleTest() {
|
|
||||||
// RemoteService has been injected into the reverser bean
|
|
||||||
given(this.remoteService.someCall()).willReturn("mock");
|
|
||||||
String reverse = reverser.reverseSomeCall();
|
|
||||||
assertThat(reverse).isEqualTo("kcom");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
NOTE: `@MockBean` cannot be used to mock the behavior of a bean that's exercised during application context refresh.
|
NOTE: `@MockBean` cannot be used to mock the behavior of a bean that's exercised during application context refresh.
|
||||||
@ -6364,42 +6287,9 @@ The `JacksonTester`, `GsonTester`, `JsonbTester`, and `BasicJsonTester` classes
|
|||||||
Any helper fields on the test class can be `@Autowired` when using `@JsonTest`.
|
Any helper fields on the test class can be `@Autowired` when using `@JsonTest`.
|
||||||
The following example shows a test class for Jackson:
|
The following example shows a test class for Jackson:
|
||||||
|
|
||||||
[source,java,pending-extract=true,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
import org.junit.jupiter.api.Test;
|
include::{include-springbootfeatures}/testing/applications/json/MyJsonTests.java[]
|
||||||
import org.springframework.beans.factory.annotation.*;
|
|
||||||
import org.springframework.boot.test.autoconfigure.json.*;
|
|
||||||
import org.springframework.boot.test.context.*;
|
|
||||||
import org.springframework.boot.test.json.*;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
|
||||||
|
|
||||||
@JsonTest
|
|
||||||
class MyJsonTests {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private JacksonTester<VehicleDetails> 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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
NOTE: JSON helper classes can also be used directly in standard unit tests.
|
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.
|
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`.
|
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))
|
include::{include-springbootfeatures}/testing/applications/json/AssertJ.java[tags=*]
|
||||||
.extractingJsonPathNumberValue("@.test.numberValue")
|
|
||||||
.satisfies((number) -> assertThat(number.floatValue()).isCloseTo(0.15f, within(0.01f)));
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
|
@ -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[]
|
||||||
|
|
||||||
|
}
|
@ -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[]
|
||||||
|
|
||||||
|
}
|
@ -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[]
|
||||||
|
|
||||||
|
}
|
@ -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) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -29,7 +29,13 @@ class MockWebTestClientTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void exampleTest(@Autowired WebTestClient webClient) {
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,13 @@ class RandomPortWebTestClientTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void exampleTest(@Autowired WebTestClient webClient) {
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
@ -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<SomeObject> 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) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<VehicleDetails> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
@ -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<Details> someRestCall(String name) {
|
||||||
|
return this.webClient.get().uri("/{name}/details", name).retrieve().bodyToMono(Details.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user