Prefix REST client test support classes w/ "MockRest"
Add "MockRest-" prefix to RequestMatchers and ResponseCreators to make it easy to find classes with static imports with Ctrl+Shift+T "MockRest" - similar to the "MockMvc" prefix on the server-side.
This commit is contained in:
@@ -18,7 +18,7 @@ package org.springframework.test.web.mock.client;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.anything;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.anything;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
import org.springframework.test.web.mock.client.match.ContentRequestMatchers;
|
||||
import org.springframework.test.web.mock.client.match.RequestMatchers;
|
||||
import org.springframework.test.web.mock.client.match.MockRestRequestMatchers;
|
||||
|
||||
/**
|
||||
* Tests for {@link ContentRequestMatchers}.
|
||||
@@ -42,36 +42,36 @@ public class ContentRequestMatchersTests {
|
||||
public void testContentType() throws Exception {
|
||||
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
RequestMatchers.content().mimeType("application/json").match(this.request);
|
||||
RequestMatchers.content().mimeType(MediaType.APPLICATION_JSON).match(this.request);
|
||||
MockRestRequestMatchers.content().mimeType("application/json").match(this.request);
|
||||
MockRestRequestMatchers.content().mimeType(MediaType.APPLICATION_JSON).match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void testContentTypeNoMatch1() throws Exception {
|
||||
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
RequestMatchers.content().mimeType("application/xml").match(this.request);
|
||||
MockRestRequestMatchers.content().mimeType("application/xml").match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void testContentTypeNoMatch2() throws Exception {
|
||||
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
RequestMatchers.content().mimeType(MediaType.APPLICATION_ATOM_XML).match(this.request);
|
||||
MockRestRequestMatchers.content().mimeType(MediaType.APPLICATION_ATOM_XML).match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
this.request.getBody().write("test".getBytes());
|
||||
|
||||
RequestMatchers.content().string("test").match(this.request);
|
||||
MockRestRequestMatchers.content().string("test").match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void testStringNoMatch() throws Exception {
|
||||
this.request.getBody().write("test".getBytes());
|
||||
|
||||
RequestMatchers.content().string("Test").match(this.request);
|
||||
MockRestRequestMatchers.content().string("Test").match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,14 +79,14 @@ public class ContentRequestMatchersTests {
|
||||
byte[] content = "test".getBytes();
|
||||
this.request.getBody().write(content);
|
||||
|
||||
RequestMatchers.content().bytes(content).match(this.request);
|
||||
MockRestRequestMatchers.content().bytes(content).match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void testBytesNoMatch() throws Exception {
|
||||
this.request.getBody().write("test".getBytes());
|
||||
|
||||
RequestMatchers.content().bytes("Test".getBytes()).match(this.request);
|
||||
MockRestRequestMatchers.content().bytes("Test".getBytes()).match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -94,14 +94,14 @@ public class ContentRequestMatchersTests {
|
||||
String content = "<foo><bar>baz</bar><bar>bazz</bar></foo>";
|
||||
this.request.getBody().write(content.getBytes());
|
||||
|
||||
RequestMatchers.content().xml(content).match(this.request);
|
||||
MockRestRequestMatchers.content().xml(content).match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void testXmlNoMatch() throws Exception {
|
||||
this.request.getBody().write("<foo>11</foo>".getBytes());
|
||||
|
||||
RequestMatchers.content().xml("<foo>22</foo>").match(this.request);
|
||||
MockRestRequestMatchers.content().xml("<foo>22</foo>").match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,7 +109,7 @@ public class ContentRequestMatchersTests {
|
||||
String content = "<foo><bar>baz</bar></foo>";
|
||||
this.request.getBody().write(content.getBytes());
|
||||
|
||||
RequestMatchers.content().node(hasXPath("/foo/bar")).match(this.request);
|
||||
MockRestRequestMatchers.content().node(hasXPath("/foo/bar")).match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
@@ -117,7 +117,7 @@ public class ContentRequestMatchersTests {
|
||||
String content = "<foo><bar>baz</bar></foo>";
|
||||
this.request.getBody().write(content.getBytes());
|
||||
|
||||
RequestMatchers.content().node(hasXPath("/foo/bar/bar")).match(this.request);
|
||||
MockRestRequestMatchers.content().node(hasXPath("/foo/bar/bar")).match(this.request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
import org.springframework.test.web.mock.client.match.RequestMatchers;
|
||||
import org.springframework.test.web.mock.client.match.MockRestRequestMatchers;
|
||||
|
||||
/**
|
||||
* Tests for {@link RequestMatchers}.
|
||||
* Tests for {@link MockRestRequestMatchers}.
|
||||
*
|
||||
* @author Craig Walls
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -45,54 +45,54 @@ public class RequestMatchersTests {
|
||||
public void requestTo() throws Exception {
|
||||
this.request.setURI(new URI("http://foo.com/bar"));
|
||||
|
||||
RequestMatchers.requestTo("http://foo.com/bar").match(this.request);
|
||||
MockRestRequestMatchers.requestTo("http://foo.com/bar").match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void requestToNoMatch() throws Exception {
|
||||
this.request.setURI(new URI("http://foo.com/bar"));
|
||||
|
||||
RequestMatchers.requestTo("http://foo.com/wrong").match(this.request);
|
||||
MockRestRequestMatchers.requestTo("http://foo.com/wrong").match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestToContains() throws Exception {
|
||||
this.request.setURI(new URI("http://foo.com/bar"));
|
||||
|
||||
RequestMatchers.requestTo(containsString("bar")).match(this.request);
|
||||
MockRestRequestMatchers.requestTo(containsString("bar")).match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void method() throws Exception {
|
||||
this.request.setMethod(HttpMethod.GET);
|
||||
|
||||
RequestMatchers.method(HttpMethod.GET).match(this.request);
|
||||
MockRestRequestMatchers.method(HttpMethod.GET).match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void methodNoMatch() throws Exception {
|
||||
this.request.setMethod(HttpMethod.POST);
|
||||
|
||||
RequestMatchers.method(HttpMethod.GET).match(this.request);
|
||||
MockRestRequestMatchers.method(HttpMethod.GET).match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void header() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
|
||||
RequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void headerMissing() throws Exception {
|
||||
RequestMatchers.header("foo", "bar").match(this.request);
|
||||
MockRestRequestMatchers.header("foo", "bar").match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void headerMissingValue() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
|
||||
RequestMatchers.header("foo", "bad").match(this.request);
|
||||
MockRestRequestMatchers.header("foo", "bad").match(this.request);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -100,13 +100,13 @@ public class RequestMatchersTests {
|
||||
public void headerContains() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
|
||||
RequestMatchers.header("foo", containsString("ba")).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test(expected=AssertionError.class)
|
||||
public void headerContainsWithMissingHeader() throws Exception {
|
||||
RequestMatchers.header("foo", containsString("baz")).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -114,26 +114,26 @@ public class RequestMatchersTests {
|
||||
public void headerContainsWithMissingValue() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
|
||||
RequestMatchers.header("foo", containsString("bx")).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headers() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
|
||||
RequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void headersWithMissingHeader() throws Exception {
|
||||
RequestMatchers.header("foo", "bar").match(this.request);
|
||||
MockRestRequestMatchers.header("foo", "bar").match(this.request);
|
||||
}
|
||||
|
||||
@Test(expected=AssertionError.class)
|
||||
public void headersWithMissingValue() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar"));
|
||||
|
||||
RequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,11 +27,11 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.client.MockClientHttpResponse;
|
||||
import org.springframework.test.web.mock.client.response.DefaultResponseCreator;
|
||||
import org.springframework.test.web.mock.client.response.ResponseCreators;
|
||||
import org.springframework.test.web.mock.client.response.MockRestResponseCreators;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* Tests for the {@link ResponseCreators} static factory methods.
|
||||
* Tests for the {@link MockRestResponseCreators} static factory methods.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
@@ -39,7 +39,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void success() throws Exception {
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) ResponseCreators.withSuccess().createResponse(null);
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) MockRestResponseCreators.withSuccess().createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertTrue(response.getHeaders().isEmpty());
|
||||
@@ -48,7 +48,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void successWithContent() throws Exception {
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withSuccess("foo", MediaType.TEXT_PLAIN);
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", MediaType.TEXT_PLAIN);
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
@@ -58,7 +58,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void successWithContentWithoutContentType() throws Exception {
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withSuccess("foo", null);
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", null);
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
@@ -69,7 +69,7 @@ public class ResponseCreatorsTests {
|
||||
@Test
|
||||
public void created() throws Exception {
|
||||
URI location = new URI("/foo");
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withCreatedEntity(location);
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withCreatedEntity(location);
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.CREATED, response.getStatusCode());
|
||||
@@ -79,7 +79,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void noContent() throws Exception {
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withNoContent();
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withNoContent();
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
|
||||
@@ -89,7 +89,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void badRequest() throws Exception {
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withBadRequest();
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withBadRequest();
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
||||
@@ -99,7 +99,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void unauthorized() throws Exception {
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withUnauthorizedRequest();
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withUnauthorizedRequest();
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||
@@ -109,7 +109,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void serverError() throws Exception {
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withServerError();
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withServerError();
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
|
||||
@@ -119,7 +119,7 @@ public class ResponseCreatorsTests {
|
||||
|
||||
@Test
|
||||
public void withStatus() throws Exception {
|
||||
DefaultResponseCreator responseCreator = ResponseCreators.withStatus(HttpStatus.FORBIDDEN);
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(HttpStatus.FORBIDDEN);
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
package org.springframework.test.web.mock.client.samples;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.method;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.ResponseCreators.withSuccess;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.method;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -17,8 +17,8 @@ package org.springframework.test.web.mock.client.samples.matchers;
|
||||
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.response.ResponseCreators.withSuccess;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
package org.springframework.test.web.mock.client.samples.matchers;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.header;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.ResponseCreators.withSuccess;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.header;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -20,10 +20,10 @@ import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.isIn;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.jsonPath;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.ResponseCreators.withSuccess;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.jsonPath;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
package org.springframework.test.web.mock.client.samples.matchers;
|
||||
|
||||
import static org.hamcrest.Matchers.hasXPath;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.ResponseCreators.withSuccess;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -21,10 +21,10 @@ import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.Matchers.lessThan;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.match.RequestMatchers.xpath;
|
||||
import static org.springframework.test.web.mock.client.response.ResponseCreators.withSuccess;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.content;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.mock.client.match.MockRestRequestMatchers.xpath;
|
||||
import static org.springframework.test.web.mock.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
|
||||
Reference in New Issue
Block a user