Commit 7bea9dd9 authored by Dave Syer's avatar Dave Syer

Massage MVC test a bit

* Use *Tests as classname
* Fixed broken test in suite where ID is not always "1"
parent 31af68a9
package org.springframework.boot.sample.ui; package org.springframework.boot.sample.ui;
import java.util.regex.Pattern;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -12,10 +15,12 @@ import org.springframework.test.web.servlet.MockMvc; ...@@ -12,10 +15,12 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/** /**
* A Basic Spring MVC Test for the Sample Controller" * A Basic Spring MVC Test for the Sample Controller"
...@@ -25,7 +30,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -25,7 +30,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = SampleWebUiApplication.class) @ContextConfiguration(classes = SampleWebUiApplication.class)
public class MessageControllerWebTest { public class MessageControllerWebTests {
@Autowired @Autowired
private WebApplicationContext wac; private WebApplicationContext wac;
...@@ -38,18 +43,42 @@ public class MessageControllerWebTest { ...@@ -38,18 +43,42 @@ public class MessageControllerWebTest {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
this.mockMvc this.mockMvc.perform(get("/")).andExpect(status().isOk())
.perform(get("/")) .andExpect(content().string(containsString("<title>Messages")));
.andExpect(status().isOk())
.andExpect(content().string(containsString("<title>Messages")));
} }
@Test @Test
public void testCreate() throws Exception { public void testCreate() throws Exception {
this.mockMvc this.mockMvc.perform(post("/").param("text", "FOO text").param("summary", "FOO"))
.perform(post("/").param("text", "FOO text").param("summary", "FOO")) .andExpect(status().isMovedTemporarily())
.andExpect(status().isMovedTemporarily()) .andExpect(header().string("location", RegexMatcher.matches("/[0-9]+")));
.andExpect(header().string("location", "/1"));
} }
private static class RegexMatcher extends TypeSafeMatcher<String> {
private final String regex;
public RegexMatcher(String regex) {
this.regex = regex;
}
public static org.hamcrest.Matcher<java.lang.String> matches(String regex) {
return new RegexMatcher(regex);
}
@Override
public boolean matchesSafely(String item) {
return Pattern.compile(this.regex).matcher(item).find();
}
@Override
public void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("was \"").appendText(item).appendText("\"");
}
@Override
public void describeTo(Description description) {
description.appendText("a string that matches regex: ")
.appendText(this.regex);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment