Skip to content

Commit e74b084

Browse files
committed
Auto-configure codec customizations and JSON when using @WebFluxTest
Closes gh-15070
1 parent ab8e4d5 commit e74b084

File tree

8 files changed

+152
-0
lines changed

8 files changed

+152
-0
lines changed

spring-boot-project/spring-boot-test-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@
179179
<artifactId>logback-classic</artifactId>
180180
<scope>test</scope>
181181
</dependency>
182+
<dependency>
183+
<groupId>com.fasterxml.jackson.module</groupId>
184+
<artifactId>jackson-module-parameter-names</artifactId>
185+
<scope>test</scope>
186+
</dependency>
182187
<dependency>
183188
<groupId>com.h2database</groupId>
184189
<artifactId>h2</artifactId>

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
2929
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3030
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
31+
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson;
3132
import org.springframework.boot.test.context.SpringBootTest;
3233
import org.springframework.boot.test.mock.mockito.MockBean;
3334
import org.springframework.context.annotation.ComponentScan;
@@ -73,6 +74,7 @@
7374
@OverrideAutoConfiguration(enabled = false)
7475
@TypeExcludeFilters(WebFluxTypeExcludeFilter.class)
7576
@AutoConfigureCache
77+
@AutoConfigureJson
7678
@AutoConfigureWebFlux
7779
@AutoConfigureWebTestClient
7880
@ImportAutoConfiguration

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ org.springframework.boot.test.autoconfigure.web.reactive.WebTestClientAutoConfig
8383

8484
# AutoConfigureWebFlux auto-configuration imports
8585
org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebFlux=\
86+
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
8687
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
8788
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
8889
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.reactive.webclient;
18+
19+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
20+
21+
/**
22+
* Example POJO used with {@link WebFluxTest} tests.
23+
*
24+
* @author Andy Wilkinson
25+
*/
26+
public class ExamplePojo {
27+
28+
private final String alpha;
29+
30+
private final String bravo;
31+
32+
public ExamplePojo(String alpha, String bravo) {
33+
this.alpha = alpha;
34+
this.bravo = bravo;
35+
}
36+
37+
public String getAlpha() {
38+
return this.alpha;
39+
}
40+
41+
public String getBravo() {
42+
return this.bravo;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.reactive.webclient;
18+
19+
import reactor.core.publisher.Mono;
20+
21+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
22+
import org.springframework.http.MediaType;
23+
import org.springframework.stereotype.Controller;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
/**
28+
* Example {@link Controller} used with {@link WebFluxTest} tests.
29+
*
30+
* @author Andy Wilkinson
31+
*/
32+
@RestController
33+
public class JsonController {
34+
35+
@GetMapping(value = "/json", produces = MediaType.APPLICATION_JSON_VALUE)
36+
public Mono<ExamplePojo> json() {
37+
return Mono.just(new ExamplePojo("a", "b"));
38+
}
39+
40+
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAllControllersIntegrationTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public void shouldFindController2() {
4848
.expectBody(String.class).isEqualTo("two");
4949
}
5050

51+
@Test
52+
public void shouldFindJsonController() {
53+
this.webClient.get().uri("/json").exchange().expectStatus().isOk();
54+
}
55+
5156
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAutoConfigurationIntegrationTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
2626
import org.springframework.context.ApplicationContext;
2727
import org.springframework.test.context.junit4.SpringRunner;
28+
import org.springframework.test.web.reactive.server.WebTestClient;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
3031
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
@@ -53,4 +54,11 @@ public void validationAutoConfigurationIsImported() {
5354
.has(importedAutoConfiguration(ValidationAutoConfiguration.class));
5455
}
5556

57+
@Test
58+
public void whatever() {
59+
WebTestClient client = this.applicationContext.getBean(WebTestClient.class);
60+
61+
System.out.println(client);
62+
}
63+
5664
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.reactive.webclient;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
24+
import org.springframework.test.context.junit4.SpringRunner;
25+
import org.springframework.test.web.reactive.server.WebTestClient;
26+
27+
/**
28+
* Tests for {@link WebFluxTest} to validate the {@link WebTestClient WebTestClient's}
29+
* codecs are customized.
30+
*
31+
* @author Andy Wilkinson
32+
*/
33+
@RunWith(SpringRunner.class)
34+
@WebFluxTest(controllers = JsonController.class)
35+
public class WebFluxTestWebTestClientCodecCustomizationIntegrationTests {
36+
37+
@Autowired
38+
private WebTestClient webClient;
39+
40+
@Test
41+
public void shouldBeAbleToCreatePojoViaParametersModule() {
42+
this.webClient.get().uri("/json").exchange().expectStatus().isOk()
43+
.expectBody(ExamplePojo.class);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)