Skip to content

Commit a6f3f18

Browse files
dmitrysulmansdeleuze
authored andcommitted
Allow supertypes in ContentResultMatchersDsl matchers
Closes gh-34542 Signed-off-by: Dmitry Sulman <dmitry.sulman@gmail.com>
1 parent 8334cb1 commit a6f3f18

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

spring-test/src/main/kotlin/org/springframework/test/web/servlet/result/ContentResultMatchersDsl.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ class ContentResultMatchersDsl internal constructor (private val actions: Result
7171
/**
7272
* @see ContentResultMatchers.string
7373
*/
74-
fun string(matcher: Matcher<String>) {
74+
fun string(matcher: Matcher<in String>) {
7575
actions.andExpect(matchers.string(matcher))
7676
}
7777

@@ -99,14 +99,14 @@ class ContentResultMatchersDsl internal constructor (private val actions: Result
9999
/**
100100
* @see ContentResultMatchers.node
101101
*/
102-
fun node(matcher: Matcher<Node>) {
102+
fun node(matcher: Matcher<in Node>) {
103103
actions.andExpect(matchers.node(matcher))
104104
}
105105

106106
/**
107107
* @see ContentResultMatchers.source
108108
*/
109-
fun source(matcher: Matcher<Source>) {
109+
fun source(matcher: Matcher<in Source>) {
110110
actions.andExpect(matchers.source(matcher))
111111
}
112112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2025 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.test.web.servlet.result
18+
19+
import io.mockk.mockk
20+
import org.hamcrest.text.CharSequenceLength.*
21+
import org.junit.jupiter.api.Test
22+
import org.springframework.mock.web.MockHttpServletRequest
23+
import org.springframework.mock.web.MockHttpServletResponse
24+
import org.springframework.test.web.servlet.*
25+
import org.springframework.web.servlet.FlashMap
26+
import org.springframework.web.servlet.ModelAndView
27+
import java.nio.charset.StandardCharsets
28+
29+
/**
30+
* Tests for [ContentResultMatchersDsl].
31+
*
32+
* @author Dmitry Sulman
33+
*/
34+
class ContentResultMatchersDslTests {
35+
36+
val mockMvc = mockk<MockMvc>()
37+
38+
@Test
39+
fun `ContentResultMatchersDsl#string accepts Matcher parameterized with String supertype`() {
40+
getStubResultActionsDsl("some string")
41+
.andExpect { content { string(hasLength(11)) } }
42+
}
43+
44+
private fun getStubResultActionsDsl(content: String): ResultActionsDsl {
45+
val resultActions = object : ResultActions {
46+
override fun andExpect(matcher: ResultMatcher): ResultActions {
47+
matcher.match(getStubMvcResult(content))
48+
return this
49+
}
50+
51+
override fun andDo(handler: ResultHandler): ResultActions {
52+
throw UnsupportedOperationException()
53+
}
54+
55+
override fun andReturn(): MvcResult {
56+
throw UnsupportedOperationException()
57+
}
58+
59+
}
60+
return ResultActionsDsl(resultActions, mockMvc)
61+
}
62+
63+
private fun getStubMvcResult(content: String): StubMvcResult {
64+
val response = MockHttpServletResponse()
65+
response.outputStream.write(content.toByteArray(StandardCharsets.UTF_8))
66+
return StubMvcResult(MockHttpServletRequest(), Any(), emptyArray(), Exception(), ModelAndView(), FlashMap(), response)
67+
}
68+
}

0 commit comments

Comments
 (0)