Add spring-test-mvc tests with Spring HATEOAS links
Issue: SPR-9886
This commit is contained in:
parent
6e3c3c58b5
commit
f30d33d32e
|
@ -595,7 +595,6 @@ project('spring-test-mvc') {
|
|||
exclude group: 'com.sun.jmx', module: 'jmxri'
|
||||
}
|
||||
testCompile "javax.servlet:jstl:1.2"
|
||||
testCompile "org.apache.tiles:tiles-jsp:2.2.2"
|
||||
testCompile "org.hibernate:hibernate-validator:4.2.0.Final"
|
||||
testCompile "org.codehaus.jackson:jackson-mapper-asl:1.4.2"
|
||||
testCompile project(":spring-oxm")
|
||||
|
@ -613,6 +612,7 @@ project('spring-test-mvc') {
|
|||
testCompile("org.springframework.security:spring-security-config:3.1.2.RELEASE") {
|
||||
exclude group: 'org.springframework'
|
||||
}
|
||||
testCompile("org.springframework.hateoas:spring-hateoas:0.3.0.RELEASE")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
package org.springframework.test.util;
|
||||
|
||||
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
||||
import static org.springframework.test.util.AssertionErrors.assertTrue;
|
||||
import static org.springframework.test.util.AssertionErrors.*;
|
||||
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
@ -85,6 +85,17 @@ public class JsonPathExpectationsHelper {
|
|||
*/
|
||||
public void assertValue(String responseContent, Object expectedValue) throws ParseException {
|
||||
Object actualValue = evaluateJsonPath(responseContent);
|
||||
if ((actualValue instanceof List) && !(expectedValue instanceof List)) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
List actualValueList = (List) actualValue;
|
||||
if (actualValueList.size() == 0) {
|
||||
fail("No matching value for JSON path \"" + this.expression + "\"");
|
||||
}
|
||||
if (actualValueList.size() != 1) {
|
||||
fail("Got a list of values " + actualValue + " instead of the value " + expectedValue);
|
||||
}
|
||||
actualValue = actualValueList.get(0);
|
||||
}
|
||||
assertEquals("JSON path" + this.expression, expectedValue, actualValue);
|
||||
}
|
||||
|
||||
|
@ -93,7 +104,7 @@ public class JsonPathExpectationsHelper {
|
|||
*/
|
||||
public void assertValueIsArray(String responseContent) throws ParseException {
|
||||
Object actualValue = evaluateJsonPath(responseContent);
|
||||
assertTrue("No value for JSON path " + this.expression, actualValue != null);
|
||||
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
|
||||
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
|
||||
assertTrue(reason, actualValue instanceof List);
|
||||
}
|
||||
|
|
|
@ -39,8 +39,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
|
|||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles2.TilesView;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesView;
|
||||
|
||||
/**
|
||||
* Tests with Java configuration.
|
||||
|
|
|
@ -18,12 +18,24 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
|
|||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
|
||||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
@ -42,6 +54,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
*/
|
||||
public class ContentAssertionTests {
|
||||
|
||||
public static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", Charset.forName("UTF-8"));
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
|
@ -51,7 +65,7 @@ public class ContentAssertionTests {
|
|||
|
||||
@Test
|
||||
public void testContentType() throws Exception {
|
||||
this.mockMvc.perform(get("/handle"))
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().contentType("text/plain"));
|
||||
|
||||
|
@ -62,29 +76,58 @@ public class ContentAssertionTests {
|
|||
|
||||
@Test
|
||||
public void testContentAsString() throws Exception {
|
||||
this.mockMvc.perform(get("/handle")).andExpect(content().string("Hello world!"));
|
||||
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
|
||||
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().string("Hello world!"));
|
||||
|
||||
this.mockMvc.perform(get("/handleUtf8"))
|
||||
.andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
|
||||
|
||||
// Hamcrest matchers...
|
||||
this.mockMvc.perform(get("/handle")).andExpect(content().string(equalTo("Hello world!")));
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)).andExpect(content().string(equalTo("Hello world!")));
|
||||
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string(equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentAsBytes() throws Exception {
|
||||
this.mockMvc.perform(get("/handle")).andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
|
||||
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
|
||||
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
|
||||
|
||||
this.mockMvc.perform(get("/handleUtf8"))
|
||||
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentStringMatcher() throws Exception {
|
||||
this.mockMvc.perform(get("/handle")).andExpect(content().string(containsString("world")));
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().string(containsString("world")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCharacterEncoding() throws Exception {
|
||||
this.mockMvc.perform(get("/handle")).andExpect(content().encoding("ISO-8859-1"));
|
||||
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().encoding("UTF-8"));
|
||||
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().encoding("ISO-8859-1"))
|
||||
.andExpect(content().string(containsString("world")));
|
||||
|
||||
this.mockMvc.perform(get("/handleUtf8"))
|
||||
.andExpect(content().encoding("UTF-8"))
|
||||
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringHateoasJsonLink() throws Exception {
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.links[?(@.rel == 'self')].href").value("http://myhost/people"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringHateoasXmlLink() throws Exception {
|
||||
Map<String, String> ns = Collections.singletonMap("ns", "http://www.w3.org/2005/Atom");
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_XML))
|
||||
.andDo(print())
|
||||
.andExpect(xpath("/person/ns:link[@rel='self']/@href", ns).string("http://myhost/people"));
|
||||
}
|
||||
|
||||
|
||||
|
@ -102,5 +145,20 @@ public class ContentAssertionTests {
|
|||
public String handleWithCharset() {
|
||||
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"; // "Hello world! (Japanese)
|
||||
}
|
||||
|
||||
@RequestMapping(value="/handle", produces={"application/json", "application/xml"})
|
||||
@ResponseBody
|
||||
public PersonResource handleJsonOrXml() {
|
||||
PersonResource resource = new PersonResource();
|
||||
resource.name = "Joe";
|
||||
resource.add(new Link("http://myhost/people"));
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
|
||||
@XmlRootElement(name="person")
|
||||
static class PersonResource extends ResourceSupport {
|
||||
String name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@
|
|||
<bean id="viewResolver"
|
||||
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
|
||||
<property name="viewClass"
|
||||
value="org.springframework.web.servlet.view.tiles2.TilesView" />
|
||||
value="org.springframework.web.servlet.view.tiles3.TilesView" />
|
||||
</bean>
|
||||
|
||||
<bean id="tilesConfigurer"
|
||||
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
|
||||
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
|
||||
<property name="definitions">
|
||||
<value>
|
||||
/WEB-INF/**/tiles.xml
|
||||
|
|
Loading…
Reference in New Issue