3

谁能给我举个例子来说明如何使用 ServletUnit 来测试 JSP 的?我需要调用 registerServlet() 吗?如果是这样,我应该通过什么类名?

4

2 回答 2

2

如果要使用默认的 Jasper 编译器,则不需要 registerServlet。但是,我需要 Jasper 罐子及其对 CLASSPATH 的依赖项。我需要获得一个基本的 JSP 来编译和渲染的 Maven 依赖项是:

<dependency>
  <groupId>tomcat</groupId>
  <artifactId>jasper</artifactId>
  <version>3.3.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>tomcat</groupId>
  <artifactId>jasper-compiler</artifactId>
  <version>5.5.23</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>tomcat</groupId>
  <artifactId>tomcat-util</artifactId>
  <version>5.5.23</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>tomcat</groupId>
  <artifactId>core_util</artifactId>
  <version>3.3.2</version>
  <scope>test</scope>
</dependency>

我被困在一个JDK1.4项目中,所以你也许可以使用更新的版本。我还没有让标准的taglib工作......

于 2009-11-25T17:06:45.113 回答
0

这就是我现在用来测试 JSP 渲染和验证表单和转发的东西。

第一个 Maven 依赖项

   <!--  Testing JSP -->
<dependency>
  <groupId>net.sourceforge.openutils</groupId>
  <artifactId>openutils-testing4web</artifactId>
  <version>1.2.1</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <artifactId>slf4j-log4j12</artifactId>
      <groupId>org.slf4j</groupId>
    </exclusion>
    <exclusion>
      <artifactId>spring-core</artifactId>
      <groupId>org.springframework</groupId>
    </exclusion>
    <exclusion>
      <artifactId>spring-context</artifactId>
      <groupId>org.springframework</groupId>
    </exclusion>
    <exclusion>
      <artifactId>slf4j-api</artifactId>
      <groupId>org.slf4j</groupId>
    </exclusion>
    <exclusion>
      <artifactId>jcl-over-slf4j</artifactId>
      <groupId>org.slf4j</groupId>
    </exclusion>
    <exclusion>
      <artifactId>jsp-api</artifactId>
      <groupId>javax.servlet</groupId>
    </exclusion>
    <exclusion>
      <artifactId>jasper-runtime</artifactId>
      <groupId>tomcat</groupId>
    </exclusion>
    <exclusion>
      <artifactId>jasper-compiler</artifactId>
      <groupId>tomcat</groupId>
    </exclusion>
    <exclusion>
      <artifactId>jasper-compiler-jdt</artifactId>
      <groupId>tomcat</groupId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>catalina</artifactId>
  <version>${tomcat.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>servlet-api</artifactId>
  <version>${tomcat.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>jasper</artifactId>
  <version>${tomcat.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>jasper-el</artifactId>
  <version>${tomcat.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>jsp-api</artifactId>
  <version>${tomcat.version}</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>${javax.servlet.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>jasper-jdt</artifactId>
  <version>6.0.29</version>
  <scope>test</scope>
</dependency>
<!-- log configuration -->

tomcat.version 是 6.0.39,你可以随意尝试现代的 tomcat 版本,7 或 8 但要关心依赖关系有时会变得有点模糊。

javax.servlet.version 是 3.0.1

接下来我定义了一个通用测试类,我的所有测试都对它进行了扩展。我每个控制器都有一个测试类。

 import it.openutils.testing.junit.AbstractDbUnitJunitSpringContextTests;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

import com.meterware.servletunit.ServletRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/integration/application-database-test.xml", "/integration/mvc-dispatcher-servlet-test.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class })
public abstract class ControllerIntegrationCommonTest extends AbstractDbUnitJunitSpringContextTests
{

  /**
   * The Web CLient for JSP rendeting Test
   */


  protected ServletRunner servletRunner;

  /**
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception
  {
    Resource web = this.applicationContext.getResource("/WEB-INF/web.xml");
    if (servletRunner == null)
    {
      servletRunner = new ServletRunner(web.getFile(),null);
    }
  }

  @After
  public void setDown() throws Exception
  {
    servletRunner.shutDown();
  }

}

@ContextConfiguration, @TestExecutionListeners 如果你想从 Spring Junit 上下文中运行它是必需的,如果你删除那么你会得到一个很好的 java.lang.IllegalStateException : Failed to load ApplicationConext。

请注意,我使用 Web.xml 实例化了 ServletRunner。这当然是强制性的,与我的制作非常相似。第二个参数 contextPath 设置为 Null。对于我的测试,我不需要。我向您推荐 API以获取更完整的信息。

配置完成后,让测试变得简单。我添加示例:

PostMethodWebRequest webRequest = new PostMethodWebRequest("http://myserver/setup/checkXML",true);
  webRequest.setParameter("param1", "11112");
  File file = new File("src/test/resources/datasets/myxml.xml");
  webRequest.selectFile("fileData",file,"multipart/form-data");

  WebResponse webResponse = servletRunner.getResponse(webRequest);

  assertNotNull(webResponse);
  assertTrue(webResponse.getURL().getPath().contains("checkXML"));
  assertNotNull(webResponse.getElementsByTagName("resultCheck"));
  log.debug(" ----------------- ");
  log.debug(webResponse.getText());
  log.debug(" ----------------- ");
  webResponse.getFormWithID("resultsForm").getSubmitButtons()[0].click();

在此示例中,我将创建一个上传文件的帖子。您需要创建 PostMethodWebRequest,并将参数 mimeencoded 设置为 true。如果不是,您将收到一些有趣的错误消息。添加文件只使用了方法。您可以在 API 中看到您可以上传一个文件或一组文件。

最后一个示例仅用于 make Get 请求

 GetMethodWebRequest webRequest = new GetMethodWebRequest("http://myserver/setup/addarea");
  webRequest.setParameter("param", "11");

  WebResponse webResponse = servletRunner.getResponse(webRequest);

  assertNotNull(webResponse);
  assertTrue(webResponse.getURL().getPath().contains("addsomething"));
  assertNotNull(webResponse.getElementsByTagName("listofsomething"));
  assertNotNull(webResponse.getElementsByTagName("someelement"));
  log.debug(" ----------------- ");
  log.debug(webResponse.getText());
  log.debug(" ----------------- ");

在这个例子中,我向我的控制器发出了一个 Get 请求。和前面一样,我发送一些参数,然后获取响应。在那里我检查 URL 是否是我所期望的,并验证 JSP 已经呈现了一些元素。

请注意,为了构建 WebRequest,我必须使用格式良好的 URL,但服务器名称没有问题,Servlet Unit 根本不使用它,您无需在任何地方定义。

我希望它对你有帮助。玩得开心!!

于 2014-05-09T13:13:10.687 回答