Polish MockRestRequestMatchers[Tests]

This commit is contained in:
Sam Brannen
2018-03-07 14:20:57 +01:00
parent f932796201
commit 2454b31b30
2 changed files with 57 additions and 27 deletions

View File

@@ -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<String, String> 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<String> 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<String> 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));
}
};

View File

@@ -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:<GET> but was:<POST>"));
}
@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:<bad> but was:<bar>"));
}
@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:<bad> but was:<bar>"));
}
@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\""));
}
}
}