import java.io.ByteArrayInputStream;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
public class HttpUnitTestLoginServlet extends TestCase {
@Before
public void setUp() throws Exception {
super.setUp();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testDoPostHttpServletRequestHttpServletResponse() {
try {
// 创建Servlet的运行环境
ServletRunner sr = new ServletRunner();
// 向环境中注册Servlet
sr.registerServlet("LoginServlet", LoginServlet.class.getName());
// 创建访问Servlet的客户端
ServletUnitClient sc = sr.newClient();
// 测试数据
String str = "sa";
ByteArrayInputStream stream = new ByteArrayInputStream(str
.getBytes());
WebRequest request = new PostMethodWebRequest(
"", stream, "text/xml");
// 发送请求
WebResponse response = sc.getResponse(request);
// 判断结果。"testuser:Hello World!"改成自己的期待结果就可以了啊。
Assert.assertTrue(response.getText()
.equals("testuser:Hello World!"));
} catch (Exception e) {
e.printStackTrace();
fail("まだ実装されていません");
}
}
}
阅读(1991) | 评论(0) | 转发(0) |