Refine hamcrest dependency in spring-test-mvc project

1) removed the hamcrest-all dependency requirement and replaced it with
the more focused hamcrest-library dependency

2) added MatcherAssertionErrors as a replacement of
org.hamcrest.MatcherAssert, which in hamcrest 1.1 is only available
through the hamcrest-all dependency (and not in hamcrest-core nor in
the hamcrest embedded in JUnit 4.4 through 4.8)

3) changed the required hamcrest version from 1.1 to 1.3 and made sure
the spring-test-mvc project does not rely on newer hamcrest
functionality without checking if it is available first

Applications that already depend on older versions of hamcrest
(in particular 1.1) via hamcrest-library, hamcrest-all or as part of
junit 4.4 through 4.8 should not be disrupted if they add spring-test
but may wish to exclude the hamcrest-library transitive dependency
from spring-test in order to avoid extra jars in the classpath

Applications that depend on hamcrest 1.3 should not have to do anything

Issue: SPR-9940
This commit is contained in:
Rossen Stoyanchev
2012-11-01 13:49:00 -04:00
parent 468f9c7814
commit 242bf7c4e3
22 changed files with 157 additions and 79 deletions

View File

@@ -17,8 +17,6 @@ package org.springframework.test.web.client.samples.matchers;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
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.client.match.MockRestRequestMatchers.content;
@@ -192,8 +190,8 @@ public class XpathRequestMatcherTests {
.andExpect(content().contentType("application/xml"))
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(4))
.andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(2))
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(lessThan(5))) // Hamcrest..
.andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(greaterThan(0))) // Hamcrest..
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(equalTo(4))) // Hamcrest..
.andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(equalTo(2))) // Hamcrest..
.andRespond(withSuccess());
this.restTemplate.put(new URI("/composers"), this.people);

View File

@@ -19,14 +19,14 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
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.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import javax.validation.Valid;
@@ -78,11 +78,10 @@ public class ModelAssertionTests {
.andExpect(model().attribute("INTEGER", nullValue()));
}
@SuppressWarnings("unchecked")
@Test
public void testAttributeHamcrestMatchers() throws Exception {
mockMvc.perform(get("/"))
.andExpect(model().attribute("integer", allOf(greaterThan(2), lessThan(4))))
.andExpect(model().attribute("integer", equalTo(3)))
.andExpect(model().attribute("string", allOf(startsWith("a string"), endsWith("value"))))
.andExpect(model().attribute("person", hasProperty("name", equalTo("a name"))));
}

View File

@@ -16,16 +16,12 @@
package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThan;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
@@ -63,8 +59,7 @@ public class StatusAssertionTests {
@Test
public void testMatcher() throws Exception {
Matcher<Integer> matcher = allOf(greaterThanOrEqualTo(400), lessThan(500));
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(matcher));
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(400)));
}
@Test

View File

@@ -16,7 +16,11 @@
package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -145,8 +149,8 @@ public class XpathAssertionTests {
this.mockMvc.perform(get("/music/people"))
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(4))
.andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(2))
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(lessThan(5))) // Hamcrest..
.andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(greaterThan(0)));
.andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(equalTo(4))) // Hamcrest..
.andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(equalTo(2)));
}
@Controller

View File

@@ -12,9 +12,9 @@
*/
package org.springframework.test.web.servlet.setup;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
import java.io.IOException;
@@ -31,7 +31,6 @@ import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockFilterConfig;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.setup.PatternMappingFilterProxy;
/**
*