【问题标题】:Can't find JSTL tag library when running test under Maven Surefire在 Maven Surefire 下运行测试时找不到 JSTL 标签库
【发布时间】: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


    【解决方案1】:

    事实证明,默认情况下,Maven 的 Surefire 插件采用了一种技巧,它创建了一个 Manifest-Only JAR 文件,以避免将完整的类路径传递给分叉的 JRE(参见 this article)。 Jetty JSP 处理器只是向每个类加载器询问其 URL 列表,因此只看到一个仅清单 JAR 文件,而不是实际使用的所有 JAR 文件,因此找不到 TLD 文件。

    该页面上也记录了简单的解决方案,即关闭useManifestOnlyJar 选项,如下所示:

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <useManifestOnlyJar>false</useManifestOnlyJar>
        </configuration>
      </plugin>
      ....
    <plugin>
    

    这指示 Surefire 将完整的类路径直接传递给分叉的 JRE 进程,从而使其能够定位 TLD。

    【讨论】:

    • 这对我在 Maven 测试期间使用带有 JSP 的 Spring-Boot 嵌入式 Tomcat 起到了作用
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 2013-12-05
    • 2023-03-09
    • 2020-12-31
    • 2021-07-07
    • 2011-07-28
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多