Skip to content

Commit 265960f

Browse files
committed
Add StatusResultMatchers.isEqualTo Kotlin extension
Issue: SPR-16429
1 parent 2c85be3 commit 265960f

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -49,13 +49,15 @@ protected StatusResultMatchers() {
4949

5050
/**
5151
* Assert the response status code with the given Hamcrest {@link Matcher}.
52+
* Use the {@code StatusResultMatchers.isEqualTo} extension in Kotlin.
5253
*/
5354
public ResultMatcher is(final Matcher<Integer> matcher) {
5455
return result -> assertThat("Response status", result.getResponse().getStatus(), matcher);
5556
}
5657

5758
/**
5859
* Assert the response status code is equal to an integer value.
60+
* Use the {@code StatusResultMatchers.isEqualTo} extension in Kotlin.
5961
*/
6062
public ResultMatcher is(final int status) {
6163
return result -> assertEquals("Response status", status, result.getResponse().getStatus());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2018 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+
* http://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 org.hamcrest.Matcher
20+
import org.springframework.test.web.servlet.ResultMatcher
21+
22+
/**
23+
* Extension for [StatusResultMatchers.is] providing an `isEqualTo` alias since `is` is
24+
* a reserved keyword in Kotlin.
25+
*
26+
* @author Sebastien Deleuze
27+
* @since 5.0.7
28+
*/
29+
fun StatusResultMatchers.isEqualTo(matcher: Matcher<Int>): ResultMatcher = `is`(matcher)
30+
31+
/**
32+
* Extension for [StatusResultMatchers.is] providing an `isEqualTo` alias since `is` is
33+
* a reserved keyword in Kotlin.
34+
*
35+
* @author Sebastien Deleuze
36+
* @since 5.0.7
37+
*/
38+
fun StatusResultMatchers.isEqualTo(status: Int): ResultMatcher = `is`(status)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2002-2018 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+
* http://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 com.nhaarman.mockito_kotlin.mock
20+
import com.nhaarman.mockito_kotlin.times
21+
import com.nhaarman.mockito_kotlin.verify
22+
import org.hamcrest.Matcher
23+
import org.junit.Test
24+
import org.junit.runner.RunWith
25+
import org.mockito.Answers
26+
import org.mockito.Mock
27+
import org.mockito.junit.MockitoJUnitRunner
28+
29+
@RunWith(MockitoJUnitRunner::class)
30+
class StatusResultMatchersExtensionsTests {
31+
32+
@Mock(answer = Answers.RETURNS_MOCKS)
33+
lateinit var matchers: StatusResultMatchers
34+
35+
@Test
36+
fun `StatusResultMatchers#is with Matcher parameter is called as expected when using isEqualTo extension`() {
37+
val matcher = mock<Matcher<Int>>()
38+
matchers.isEqualTo(matcher)
39+
verify(matchers, times(1)).`is`(matcher)
40+
}
41+
42+
@Test
43+
fun `StatusResultMatchers#is with int parameter is called as expected when using isEqualTo extension`() {
44+
matchers.isEqualTo(200)
45+
verify(matchers, times(1)).`is`(200)
46+
}
47+
48+
}

0 commit comments

Comments
 (0)