Skip to content

Commit 4c60562

Browse files
committed
Ensure the WebClient to be present before enabling the WebTestClientContextCustomizer
1 parent b397032 commit 4c60562

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/server/WebTestClientContextCustomizerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ContextCustomizer createContextCustomizer(Class<?> testClass,
3939
List<ContextConfigurationAttributes> configAttributes) {
4040
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
4141
SpringBootTest.class);
42-
return (springBootTest != null) ? new WebTestClientContextCustomizer() : null;
42+
return (springBootTest != null && isWebClientPresent()) ? new WebTestClientContextCustomizer() : null;
4343
}
4444

4545
private boolean isWebClientPresent() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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+
package org.springframework.boot.test.web.reactive.server;
17+
18+
import java.util.Collections;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.test.context.ContextCustomizer;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link WebTestClientContextCustomizerFactory} when spring webflux is on the
30+
* classpath.
31+
*
32+
* @author Tobias Gesellchen
33+
*/
34+
class WebTestClientContextCustomizerFactoryWithWebfluxTest {
35+
36+
WebTestClientContextCustomizerFactory contextCustomizerFactory;
37+
38+
@BeforeEach
39+
void setup() {
40+
this.contextCustomizerFactory = new WebTestClientContextCustomizerFactory();
41+
}
42+
43+
@Test
44+
void createContextCustomizer() {
45+
ContextCustomizer contextCustomizer = this.contextCustomizerFactory.createContextCustomizer(TestClass.class,
46+
Collections.emptyList());
47+
assertThat(contextCustomizer).isNotNull();
48+
}
49+
50+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
51+
static class TestClass {
52+
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2019 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+
* https://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+
package org.springframework.boot.test.web.reactive.server;
17+
18+
import java.util.Collections;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
25+
import org.springframework.test.context.ContextCustomizer;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link WebTestClientContextCustomizerFactory} when spring webflux is not on
31+
* the classpath.
32+
*
33+
* @author Tobias Gesellchen
34+
*/
35+
@ClassPathExclusions("spring-webflux*.jar")
36+
public class WebTestClientContextCustomizerFactoryWithoutWebfluxTest {
37+
38+
WebTestClientContextCustomizerFactory contextCustomizerFactory;
39+
40+
@BeforeEach
41+
void setup() {
42+
this.contextCustomizerFactory = new WebTestClientContextCustomizerFactory();
43+
}
44+
45+
@Test
46+
void doNotCreateContextCustomizer() {
47+
ContextCustomizer contextCustomizer = this.contextCustomizerFactory.createContextCustomizer(TestClass.class,
48+
Collections.emptyList());
49+
assertThat(contextCustomizer).isNull();
50+
}
51+
52+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
53+
static class TestClass {
54+
55+
}
56+
57+
}

0 commit comments

Comments
 (0)