Skip to content

Commit 3426e62

Browse files
committed
Add MockMvcTestClient
See gh-19647
1 parent 128acaf commit 3426e62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+5299
-58
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2002-2020 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.test.web.servlet.client;
17+
18+
import javax.servlet.Filter;
19+
20+
import org.springframework.http.client.reactive.ClientHttpConnector;
21+
import org.springframework.test.web.reactive.server.WebTestClient;
22+
import org.springframework.test.web.servlet.DispatcherServletCustomizer;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
import org.springframework.test.web.servlet.RequestBuilder;
25+
import org.springframework.test.web.servlet.ResultMatcher;
26+
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
27+
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
28+
29+
/**
30+
* Base class for implementations of {@link MockMvcTestClient.MockMvcServerSpec}
31+
* that simply delegates to a {@link ConfigurableMockMvcBuilder} supplied by
32+
* the concrete sub-classes.
33+
*
34+
* @author Rossen Stoyanchev
35+
* @since 5.3
36+
* @param <B> the type of the concrete sub-class spec
37+
*/
38+
abstract class AbstractMockMvcServerSpec<B extends MockMvcTestClient.MockMvcServerSpec<B>>
39+
implements MockMvcTestClient.MockMvcServerSpec<B> {
40+
41+
@Override
42+
public <T extends B> T filters(Filter... filters) {
43+
getMockMvcBuilder().addFilters(filters);
44+
return self();
45+
}
46+
47+
public final <T extends B> T filter(Filter filter, String... urlPatterns) {
48+
getMockMvcBuilder().addFilter(filter, urlPatterns);
49+
return self();
50+
}
51+
52+
@Override
53+
public <T extends B> T defaultRequest(RequestBuilder requestBuilder) {
54+
getMockMvcBuilder().defaultRequest(requestBuilder);
55+
return self();
56+
}
57+
58+
@Override
59+
public <T extends B> T alwaysExpect(ResultMatcher resultMatcher) {
60+
getMockMvcBuilder().alwaysExpect(resultMatcher);
61+
return self();
62+
}
63+
64+
@Override
65+
public <T extends B> T dispatchOptions(boolean dispatchOptions) {
66+
getMockMvcBuilder().dispatchOptions(dispatchOptions);
67+
return self();
68+
}
69+
70+
@Override
71+
public <T extends B> T dispatcherServletCustomizer(DispatcherServletCustomizer customizer) {
72+
getMockMvcBuilder().addDispatcherServletCustomizer(customizer);
73+
return self();
74+
}
75+
76+
@Override
77+
public <T extends B> T apply(MockMvcConfigurer configurer) {
78+
getMockMvcBuilder().apply(configurer);
79+
return self();
80+
}
81+
82+
@SuppressWarnings("unchecked")
83+
private <T extends B> T self() {
84+
return (T) this;
85+
}
86+
87+
/**
88+
* Return the concrete {@link ConfigurableMockMvcBuilder} to delegate
89+
* configuration methods and to use to create the {@link MockMvc}.
90+
*/
91+
protected abstract ConfigurableMockMvcBuilder<?> getMockMvcBuilder();
92+
93+
@Override
94+
public WebTestClient.Builder configureClient() {
95+
MockMvc mockMvc = getMockMvcBuilder().build();
96+
ClientHttpConnector connector = new MockMvcHttpConnector(mockMvc);
97+
return WebTestClient.bindToServer(connector);
98+
}
99+
100+
@Override
101+
public WebTestClient build() {
102+
return configureClient().build();
103+
}
104+
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2020 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.test.web.servlet.client;
17+
18+
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
19+
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
20+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
21+
import org.springframework.web.context.WebApplicationContext;
22+
23+
/**
24+
* Simple wrapper around a {@link DefaultMockMvcBuilder}.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 5.3
28+
*/
29+
class ApplicationContextMockMvcSpec extends AbstractMockMvcServerSpec<ApplicationContextMockMvcSpec> {
30+
31+
private final DefaultMockMvcBuilder mockMvcBuilder;
32+
33+
34+
public ApplicationContextMockMvcSpec(WebApplicationContext context) {
35+
this.mockMvcBuilder = MockMvcBuilders.webAppContextSetup(context);
36+
}
37+
38+
@Override
39+
protected ConfigurableMockMvcBuilder<?> getMockMvcBuilder() {
40+
return this.mockMvcBuilder;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)