Skip to content

Commit 40c5c5d

Browse files
ngocnhan-tran1996sdeleuze
authored andcommitted
Add Kotlin code samples for KT-22208
Closes gh-35820 Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent 6b8552b commit 40c5c5d

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/expectations.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ Kotlin::
184184
+
185185
[source,kotlin,indent=0,subs="verbatim,quotes"]
186186
----
187-
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
187+
standaloneSetup(SimpleController())
188+
.alwaysExpect<StandaloneMockMvcBuilder>(MockMvcResultMatchers.status().isOk())
189+
.alwaysExpect<StandaloneMockMvcBuilder>(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
190+
.build()
188191
----
189192
======
190193

framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/filters.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Kotlin::
1818
+
1919
[source,kotlin,indent=0,subs="verbatim,quotes"]
2020
----
21-
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
21+
mockMvc = standaloneSetup(PersonController()).addFilters<StandaloneMockMvcBuilder>(CharacterEncodingFilter()).build()
2222
----
2323
======
2424

framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/requests.adoc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,18 @@ Kotlin::
159159
+
160160
[source,kotlin,indent=0,subs="verbatim,quotes"]
161161
----
162-
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
162+
class MyWebTests {
163+
164+
lateinit var mockMvc: MockMvc
165+
166+
@BeforeEach
167+
fun setup() {
168+
mockMvc = MockMvcBuilders.standaloneSetup(AccountController())
169+
.defaultRequest<StandaloneMockMvcBuilder>(get("/")
170+
.contextPath("/app").servletPath("/main")
171+
.accept(MediaType.APPLICATION_JSON)).build()
172+
}
173+
}
163174
----
164175
======
165176

framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/setup-steps.adoc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ Kotlin::
2525
+
2626
[source,kotlin,indent=0,subs="verbatim,quotes"]
2727
----
28-
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
28+
// static import of MockMvcBuilders.standaloneSetup
29+
30+
val mockMvc = standaloneSetup(MusicController())
31+
.defaultRequest<StandaloneMockMvcBuilder>(get("/").accept(MediaType.APPLICATION_JSON))
32+
.alwaysExpect<StandaloneMockMvcBuilder>(status().isOk())
33+
.alwaysExpect<StandaloneMockMvcBuilder>(content().contentType("application/json;charset=UTF-8"))
34+
.build()
2935
----
3036
======
3137

@@ -53,7 +59,13 @@ Kotlin::
5359
+
5460
[source,kotlin,indent=0,subs="verbatim,quotes"]
5561
----
56-
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
62+
// static import of SharedHttpSessionConfigurer.sharedHttpSession
63+
64+
val mockMvc = MockMvcBuilders.standaloneSetup(TestController())
65+
.apply<StandaloneMockMvcBuilder>(sharedHttpSession())
66+
.build()
67+
68+
// Use mockMvc to perform requests...
5769
----
5870
======
5971

framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/mah.adoc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,19 @@ Kotlin::
267267
+
268268
[source,kotlin,indent=0,subs="verbatim,quotes"]
269269
----
270-
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
270+
val mockMvc = MockMvcBuilders
271+
.webAppContextSetup(context)
272+
.apply(springSecurity())
273+
.build()
274+
275+
webClient = MockMvcWebClientBuilder
276+
.mockMvcSetup(mockMvc)
277+
// for illustration only - defaults to ""
278+
.contextPath("")
279+
// By default MockMvc is used for localhost only;
280+
// the following will use MockMvc for example.com and example.org as well
281+
.useMockMvcForHosts("example.com", "example.org")
282+
.build()
271283
----
272284
======
273285

framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,19 @@ Kotlin::
562562
+
563563
[source,kotlin,indent=0,subs="verbatim,quotes"]
564564
----
565-
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
565+
val mockMvc: MockMvc = MockMvcBuilders
566+
.webAppContextSetup(context)
567+
.apply(springSecurity())
568+
.build()
569+
570+
driver = MockMvcHtmlUnitDriverBuilder
571+
.mockMvcSetup(mockMvc)
572+
// for illustration only - defaults to ""
573+
.contextPath("")
574+
// By default MockMvc is used for localhost only;
575+
// the following will use MockMvc for example.com and example.org as well
576+
.useMockMvcForHosts("example.com", "example.org")
577+
.build()
566578
----
567579
======
568580

0 commit comments

Comments
 (0)