Last active
August 29, 2015 14:23
-
-
Save mattwynne/02f2e0856f8cd862e58d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package shouty.web; | |
| import com.mockrunner.mock.web.MockFilterConfig; | |
| import com.mockrunner.mock.web.MockHttpServletRequest; | |
| import com.mockrunner.mock.web.WebMockObjectFactory; | |
| import com.mockrunner.servlet.ServletTestModule; | |
| import org.junit.Before; | |
| import spark.servlet.SparkApplication; | |
| import spark.servlet.SparkFilter; | |
| import javax.servlet.Filter; | |
| import javax.servlet.FilterConfig; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.http.HttpServletResponse; | |
| /** | |
| * This class hooks up our Spark app with Mockrunner - | |
| * a library for unit testing servlets (as well as other | |
| * J2EE APIs). | |
| * <p> | |
| * If you're using a different web framework than Spark, | |
| * you should still be able to use Mockrunner - you just | |
| * have to wire it up slightly differently. | |
| */ | |
| public abstract class BaseServerTest { | |
| private ServletTestModule tester; | |
| private WebMockObjectFactory factory; | |
| @Before | |
| public void createStubContainer() throws ServletException { | |
| factory = new WebMockObjectFactory(); | |
| tester = new ServletTestModule(factory); | |
| MockFilterConfig filterConfig = new MockFilterConfig(); | |
| Filter sparkFilter = new SparkFilter() { | |
| @Override | |
| protected SparkApplication getApplication(FilterConfig filterConfig) throws ServletException { | |
| return createServer(); | |
| } | |
| }; | |
| sparkFilter.init(filterConfig); | |
| tester.addFilter(sparkFilter); | |
| tester.setDoChain(true); | |
| factory.getMockRequest().setContextPath(""); | |
| } | |
| protected abstract SparkApplication createServer(); | |
| protected void get(String requestUri) { | |
| MockHttpServletRequest req = factory.getMockRequest(); | |
| req.setRequestURI(requestUri); | |
| tester.doGet(); | |
| } | |
| protected String getResponseBody() { | |
| return tester.getOutput(); | |
| } | |
| protected HttpServletResponse getResponse() { | |
| return (HttpServletResponse) tester.getFilteredResponse(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package shouty.web; | |
| import org.junit.Test; | |
| import shouty.core.Person; | |
| import spark.servlet.SparkApplication; | |
| import javax.servlet.ServletException; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import static org.junit.Assert.assertEquals; | |
| public class ServerTest extends BaseServerTest { | |
| private final Map<String, Person> people = new HashMap<>(); | |
| @Override | |
| protected SparkApplication createServer() { | |
| return new Server(people); | |
| } | |
| @Test | |
| public void shouldRespondWithHello() throws ServletException { | |
| get("/"); | |
| assertEquals("Hello World", getResponseBody()); | |
| assertEquals(200, getResponse().getStatus()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment