diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java index 305126c65f..36670ba013 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java @@ -45,6 +45,7 @@ import static org.springframework.test.util.AssertionErrors.assertTrue; * * @author Craig Walls * @author Rossen Stoyanchev + * @author Sam Brannen * @since 3.2 */ public abstract class MockRestRequestMatchers { @@ -132,7 +133,7 @@ public abstract class MockRestRequestMatchers { MultiValueMap params = getQueryParams(request); assertValueCount("query param", name, params, expectedValues.length); for (int i = 0 ; i < expectedValues.length; i++) { - assertEquals("Query param + [" + name + "]", expectedValues[i], params.get(name).get(i)); + assertEquals("Query param [" + name + "]", expectedValues[i], params.get(name).get(i)); } }; } @@ -163,7 +164,7 @@ public abstract class MockRestRequestMatchers { List headerValues = request.getHeaders().get(name); Assert.state(headerValues != null, "No header values"); for (int i = 0; i < matchers.length; i++) { - assertThat("Request header[" + name + "]", headerValues.get(i), matchers[i]); + assertThat("Request header [" + name + "]", headerValues.get(i), matchers[i]); } }; } @@ -177,7 +178,7 @@ public abstract class MockRestRequestMatchers { List headerValues = request.getHeaders().get(name); Assert.state(headerValues != null, "No header values"); for (int i = 0; i < expectedValues.length; i++) { - assertEquals("Request header [" + name + "]", + assertEquals("Request header [" + name + "]", expectedValues[i], headerValues.get(i)); } }; diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java index c2d7f9c680..8477279342 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -25,13 +25,16 @@ import org.junit.Test; import org.springframework.http.HttpMethod; import org.springframework.mock.http.client.MockClientHttpRequest; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit tests for {@link MockRestRequestMatchers}. * * @author Craig Walls * @author Rossen Stoyanchev + * @author Sam Brannen */ public class MockRestRequestMatchersTests { @@ -52,11 +55,12 @@ public class MockRestRequestMatchersTests { MockRestRequestMatchers.requestToUriTemplate("http://foo.com/{bar}", "bar").match(this.request); } - @Test(expected = AssertionError.class) + @Test public void requestToNoMatch() throws Exception { this.request.setURI(new URI("http://foo.com/bar")); - MockRestRequestMatchers.requestTo("http://foo.com/wrong").match(this.request); + assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.requestTo("http://foo.com/wrong").match(this.request)); } @Test @@ -73,11 +77,13 @@ public class MockRestRequestMatchersTests { MockRestRequestMatchers.method(HttpMethod.GET).match(this.request); } - @Test(expected = AssertionError.class) + @Test public void methodNoMatch() throws Exception { this.request.setMethod(HttpMethod.POST); - MockRestRequestMatchers.method(HttpMethod.GET).match(this.request); + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request)); + assertThat(error.getMessage(), containsString("expected: but was:")); } @Test @@ -87,16 +93,20 @@ public class MockRestRequestMatchersTests { MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); } - @Test(expected = AssertionError.class) + @Test public void headerMissing() throws Exception { - MockRestRequestMatchers.header("foo", "bar").match(this.request); + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); + assertThat(error.getMessage(), containsString("was null")); } - @Test(expected = AssertionError.class) + @Test public void headerMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); - MockRestRequestMatchers.header("foo", "bad").match(this.request); + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.header("foo", "bad").match(this.request)); + assertThat(error.getMessage(), containsString("expected: but was:")); } @Test @@ -106,16 +116,20 @@ public class MockRestRequestMatchersTests { MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request); } - @Test(expected = AssertionError.class) + @Test public void headerContainsWithMissingHeader() throws Exception { - MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request); + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)); + assertThat(error.getMessage(), containsString("but was null")); } - @Test(expected = AssertionError.class) + @Test public void headerContainsWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); - MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request); + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request)); + assertThat(error.getMessage(), containsString("was \"bar\"")); } @Test @@ -125,46 +139,61 @@ public class MockRestRequestMatchersTests { MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); } - @Test(expected = AssertionError.class) + @Test public void headersWithMissingHeader() throws Exception { - MockRestRequestMatchers.header("foo", "bar").match(this.request); + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); + assertThat(error.getMessage(), containsString("but was null")); } - @Test(expected = AssertionError.class) + @Test public void headersWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Collections.singletonList("bar")); - MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request)); + assertThat(error.getMessage(), containsString("to have at least <2> values")); } @Test public void queryParam() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); + MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request); } - @Test(expected = AssertionError.class) + @Test public void queryParamMissing() throws Exception { this.request.setURI(new URI("http://foo.com/a")); - MockRestRequestMatchers.queryParam("foo", "bar").match(this.request); + + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request)); + assertThat(error.getMessage(), containsString("but was null")); } - @Test(expected = AssertionError.class) + @Test public void queryParamMissingValue() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); - MockRestRequestMatchers.queryParam("foo", "bad").match(this.request); + + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request)); + assertThat(error.getMessage(), containsString("expected: but was:")); } @Test public void queryParamContains() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); + MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request); } - @Test(expected = AssertionError.class) + @Test public void queryParamContainsWithMissingValue() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); - MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request); + + AssertionError error = assertThrows(AssertionError.class, + () -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request)); + assertThat(error.getMessage(), containsString("was \"bar\"")); } -} \ No newline at end of file +}