Keep on moving

あんまりまとまってないことを書きますよ

servletのテスト

ServletなものへのテストはServletTesterを使って以下のように書くらしい。
ちなみにJUnit 4.10 + Jetty 7.Xでの書き方です。

http://docs.codehaus.org/display/JETTY/ServletTester

import org.junit.Before;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.testing.ServletTester;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class WebAppTest {
	private ServletTester tester;
	private HttpTester request;
	private HttpTester response;

	@Before
	public void setUp() throws Exception {
		this.tester = new ServletTester();
		this.tester.setContextPath("/");

		this.tester.addServlet(ApiWebAppServlet.class, "/api/*");
		this.tester.start();

		this.request = new HttpTester();
		this.response = new HttpTester();
		this.request.setMethod("GET");
		this.request.setHeader("Host", "tester");
		this.request.setVersion("HTTP/1.1");
	}
	@Test
	public void testHomepage() throws Exception {
		this.request.setURI("/api/node/");
		this.response.parse(tester.getResponses(request.generate()));
		assertTrue(this.response.getMethod() == null);
		assertEquals(200, this.response.getStatus());
		assertEquals("Hello World", this.response.getContent());
	}
}