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

@@ -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\""));
}
}
}