Skip to content

Commit c63dbe7

Browse files
committed
Document LocalTestWebServer
Closes gh-48333
1 parent e9db595 commit c63dbe7

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

documentation/spring-boot-docs/src/docs/antora/modules/how-to/pages/webserver.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ Contrary to a test, application code callbacks are processed early (before the v
165165

166166

167167

168+
[[howto.webserver.build-uri-test-web-server]]
169+
== Build URI Against the Test Web Server
170+
171+
Tests that start a javadoc:org.springframework.boot.web.server.WebServer[] such as `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can get information about
172+
the running server using javadoc:org.springframework.boot.test.http.server.LocalTestWebServer[], as shown in the following example:
173+
174+
include-code::MyWebIntegrationTests[]
175+
176+
javadoc:org.springframework.boot.test.http.server.LocalTestWebServer[] provides access to a suitable javadoc:org.springframework.web.util.UriBuilderFactory[].
177+
It can also be customized to build such an instance on a sub-path of your application.
178+
179+
180+
168181
[[howto.webserver.enable-response-compression]]
169182
== Enable HTTP Response Compression
170183

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-present 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+
17+
package org.springframework.boot.docs.howto.webserver.builduritestwebserver;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
24+
import org.springframework.boot.test.http.server.LocalTestWebServer;
25+
import org.springframework.context.ApplicationContext;
26+
27+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
28+
class MyWebIntegrationTests {
29+
30+
@Autowired
31+
ApplicationContext context;
32+
33+
// ...
34+
35+
@Test
36+
void test() {
37+
String urlToTest = LocalTestWebServer.obtain(this.context).uri("/test");
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-present 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+
17+
package org.springframework.boot.docs.howto.webserver.builduritestwebserver
18+
19+
import org.junit.jupiter.api.Test
20+
import org.springframework.beans.factory.annotation.Autowired
21+
import org.springframework.boot.test.context.SpringBootTest
22+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
23+
import org.springframework.boot.test.http.server.LocalTestWebServer
24+
import org.springframework.context.ApplicationContext
25+
26+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
27+
class MyWebIntegrationTests(@Autowired val context: ApplicationContext) {
28+
29+
// ...
30+
31+
@Test
32+
fun test() {
33+
val urlToTest = LocalTestWebServer.obtain(this.context).uri("/test")
34+
}
35+
36+
}
37+

0 commit comments

Comments
 (0)