diff --git a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDsl.kt b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDsl.kt index f8322e40ddff..7a1277d1c4ba 100644 --- a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDsl.kt +++ b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDsl.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -71,7 +71,7 @@ class ContentResultMatchersDsl internal constructor (private val actions: Result /** * @see ContentResultMatchers.string */ - fun string(matcher: Matcher) { + fun string(matcher: Matcher) { actions.andExpect(matchers.string(matcher)) } @@ -99,14 +99,14 @@ class ContentResultMatchersDsl internal constructor (private val actions: Result /** * @see ContentResultMatchers.node */ - fun node(matcher: Matcher) { + fun node(matcher: Matcher) { actions.andExpect(matchers.node(matcher)) } /** * @see ContentResultMatchers.source */ - fun source(matcher: Matcher) { + fun source(matcher: Matcher) { actions.andExpect(matchers.source(matcher)) } diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDslTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDslTests.kt new file mode 100644 index 000000000000..1a186b6b63d5 --- /dev/null +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDslTests.kt @@ -0,0 +1,68 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.web.servlet.result + +import io.mockk.mockk +import org.hamcrest.text.CharSequenceLength.* +import org.junit.jupiter.api.Test +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.test.web.servlet.* +import org.springframework.web.servlet.FlashMap +import org.springframework.web.servlet.ModelAndView +import java.nio.charset.StandardCharsets + +/** + * Tests for [ContentResultMatchersDsl]. + * + * @author Dmitry Sulman + */ +class ContentResultMatchersDslTests { + + val mockMvc = mockk() + + @Test + fun `ContentResultMatchersDsl#string accepts Matcher parameterized with String supertype`() { + getStubResultActionsDsl("some string") + .andExpect { content { string(hasLength(11)) } } + } + + private fun getStubResultActionsDsl(content: String): ResultActionsDsl { + val resultActions = object : ResultActions { + override fun andExpect(matcher: ResultMatcher): ResultActions { + matcher.match(getStubMvcResult(content)) + return this + } + + override fun andDo(handler: ResultHandler): ResultActions { + throw UnsupportedOperationException() + } + + override fun andReturn(): MvcResult { + throw UnsupportedOperationException() + } + + } + return ResultActionsDsl(resultActions, mockMvc) + } + + private fun getStubMvcResult(content: String): StubMvcResult { + val response = MockHttpServletResponse() + response.outputStream.write(content.toByteArray(StandardCharsets.UTF_8)) + return StubMvcResult(MockHttpServletRequest(), Any(), emptyArray(), Exception(), ModelAndView(), FlashMap(), response) + } +}