【发布时间】:2015-12-26 10:53:37
【问题描述】:
我有一个基于 Servlet 的应用程序(OAuth 实现),它将一些响应呈现委托给 JSP,如下例所示:
private void doLoginPage( AuthorizationSession authzSession, String errorMsg, HttpServletRequest request, HttpServletResponse response ) throws OAuthSystemException {
try {
response.setHeader( HTTP.CONTENT_TYPE, ContentType.create( "text/html", "utf-8" ).toString() );
request.getRequestDispatcher( "/WEB-INF/OAuthLogin.jsp" ).include( request, response );
} catch ( Throwable e ) {
throw new OAuthSystemException( "Error generating login page", e );
}
}
这是 JSP 文件(简化):
<%@ page contentType="text/html;charset=UTF-8" language="java" session="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="errorMsg" scope="request" class="java.lang.String"/>
<html>
<head>
<title>Sign In</title>
</head>
<body style="text-align: center;">
<h1>Sign In</h1>
<div style="border: 1px black;">
<c:if test="${!(empty errorMsg)}">
<p style="color:red;">${errorMsg}</p>
</c:if>
<form method="post" action="<c:url value="/authorize"/>">
<div><label for="email">Email:</label></div>
<div><input type="text" name="email" id="email"/></div>
<div><label for="password">Password:</label></div>
<div><input type="password" name="password" id="password"/></div>
<div><input type="submit" title="Sign In" /></div>
</form>
</div>
</body>
</html>
我已经设置好单元测试,以便可以运行基于服务器的测试 作为单元测试,注入模拟,使用 Jetty 作为进程内服务器 (好吧,所以这不是纯粹的单元测试)。
作为单元测试的一部分,我编写了一个测试来确保页面在适当的时候被呈现,并且它包含某些关键属性(我正在使用 TestNG 和 Hamcrest):
@Test
public void testRequestGrantYieldsLoginPage() throws Exception {
HttpGet request = new HttpGet( String.format( "%s/authorize?client_id=%s&redirect_uri=%s&response_type=token",
serverConnector.getServerURL(),
"*******secret*****",
URLEncoder.encode( "*****secret*********", "UTF-8" )));
DefaultHttpClient httpClient = new SystemDefaultHttpClient();
HttpResponse response = httpClient.execute( request );
assertThat( response, is( notNullValue() ));
assertThat( response.getStatusLine().getStatusCode(), is( 200 ));
assertThat( response.getEntity().getContentType().toString(), containsString( "text/html" ));
String body = IOUtils.toString( response.getEntity().getContent() );
assertThat( body, allOf(
is( notNullValue()),
containsString( "value=\"gQwCAShitcuP-_2OY58lgw3YW0AfbLE8m62mrvXWvQbiDLJk9QnDTs7pc0HH\"" )
));
}
当我从我的 IDE (IntelliJ) 运行我的单元测试时,这工作正常,但是当我在 Maven 的 Surefire 下运行它们时,这个特定的测试失败:服务器线程抛出如下异常:
org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
这当然会导致测试的客户端失败,因为它会返回错误响应而不是预期的登录页面。
那么这两个测试环境之间有什么不同,我怎样才能让这个测试在这两个环境中工作?
【问题讨论】:
标签: java jsp maven jetty surefire