From 40c5c5d5f01439f73843b5079f6678fe724552f9 Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Fri, 14 Nov 2025 22:44:10 +0700 Subject: [PATCH] Add Kotlin code samples for KT-22208 Closes gh-35820 Signed-off-by: Tran Ngoc Nhan --- .../testing/mockmvc/hamcrest/expectations.adoc | 5 ++++- .../pages/testing/mockmvc/hamcrest/filters.adoc | 2 +- .../pages/testing/mockmvc/hamcrest/requests.adoc | 13 ++++++++++++- .../testing/mockmvc/hamcrest/setup-steps.adoc | 16 ++++++++++++++-- .../ROOT/pages/testing/mockmvc/htmlunit/mah.adoc | 14 +++++++++++++- .../testing/mockmvc/htmlunit/webdriver.adoc | 14 +++++++++++++- 6 files changed, 57 insertions(+), 7 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/expectations.adoc b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/expectations.adoc index 5206b34d97f..e8a81fe2c61 100644 --- a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/expectations.adoc +++ b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/expectations.adoc @@ -184,7 +184,10 @@ Kotlin:: + [source,kotlin,indent=0,subs="verbatim,quotes"] ---- - // Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed + standaloneSetup(SimpleController()) + .alwaysExpect(MockMvcResultMatchers.status().isOk()) + .alwaysExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) + .build() ---- ====== diff --git a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/filters.adoc b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/filters.adoc index d61a9f1b174..ea625acabc5 100644 --- a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/filters.adoc +++ b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/filters.adoc @@ -18,7 +18,7 @@ Kotlin:: + [source,kotlin,indent=0,subs="verbatim,quotes"] ---- - // Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed + mockMvc = standaloneSetup(PersonController()).addFilters(CharacterEncodingFilter()).build() ---- ====== diff --git a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/requests.adoc b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/requests.adoc index cc85e2846ad..5bc0f331d35 100644 --- a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/requests.adoc +++ b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/requests.adoc @@ -159,7 +159,18 @@ Kotlin:: + [source,kotlin,indent=0,subs="verbatim,quotes"] ---- - // Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed + class MyWebTests { + + lateinit var mockMvc: MockMvc + + @BeforeEach + fun setup() { + mockMvc = MockMvcBuilders.standaloneSetup(AccountController()) + .defaultRequest(get("/") + .contextPath("/app").servletPath("/main") + .accept(MediaType.APPLICATION_JSON)).build() + } + } ---- ====== diff --git a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/setup-steps.adoc b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/setup-steps.adoc index 8cda0b66b03..9d895e9de9b 100644 --- a/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/setup-steps.adoc +++ b/framework-docs/modules/ROOT/pages/testing/mockmvc/hamcrest/setup-steps.adoc @@ -25,7 +25,13 @@ Kotlin:: + [source,kotlin,indent=0,subs="verbatim,quotes"] ---- - // Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed + // static import of MockMvcBuilders.standaloneSetup + + val mockMvc = standaloneSetup(MusicController()) + .defaultRequest(get("/").accept(MediaType.APPLICATION_JSON)) + .alwaysExpect(status().isOk()) + .alwaysExpect(content().contentType("application/json;charset=UTF-8")) + .build() ---- ====== @@ -53,7 +59,13 @@ Kotlin:: + [source,kotlin,indent=0,subs="verbatim,quotes"] ---- - // Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed + // static import of SharedHttpSessionConfigurer.sharedHttpSession + + val mockMvc = MockMvcBuilders.standaloneSetup(TestController()) + .apply(sharedHttpSession()) + .build() + + // Use mockMvc to perform requests... ---- ====== diff --git a/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/mah.adoc b/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/mah.adoc index 97dd4171b95..e78e83787c8 100644 --- a/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/mah.adoc +++ b/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/mah.adoc @@ -267,7 +267,19 @@ Kotlin:: + [source,kotlin,indent=0,subs="verbatim,quotes"] ---- - // Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed + val mockMvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build() + + webClient = MockMvcWebClientBuilder + .mockMvcSetup(mockMvc) + // for illustration only - defaults to "" + .contextPath("") + // By default MockMvc is used for localhost only; + // the following will use MockMvc for example.com and example.org as well + .useMockMvcForHosts("example.com", "example.org") + .build() ---- ====== diff --git a/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc b/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc index a9e3533cac5..ea1575a9353 100644 --- a/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc +++ b/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc @@ -562,7 +562,19 @@ Kotlin:: + [source,kotlin,indent=0,subs="verbatim,quotes"] ---- - // Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed + val mockMvc: MockMvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build() + + driver = MockMvcHtmlUnitDriverBuilder + .mockMvcSetup(mockMvc) + // for illustration only - defaults to "" + .contextPath("") + // By default MockMvc is used for localhost only; + // the following will use MockMvc for example.com and example.org as well + .useMockMvcForHosts("example.com", "example.org") + .build() ---- ======