簡體   English   中英

使用ServletUnit測試JSP的示例

[英]example of using ServletUnit to test JSP's

有人能指出我如何使用ServletUnit來測試JSP的例子嗎? 我需要調用registerServlet()嗎? 如果是這樣,我會通過什么類名?

如果要使用默認的Jasper編譯器,則不需要registerServlet。 但是,我需要Jasper jar及其對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工作......

這就是我現在用來測試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();
  }

}

如果要從Spring Junit上下文中運行它,則需要@ContextConfiguration,@ TestExecutionListeners,如果刪除則會得到一個很好的java.lang.IllegalStateException:無法加載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();

在這個例子中,我將發布一個文件上傳文件。 您需要使用參數mimeencoded set as true創建PostMethodWebRequest。 如果不是,您將收到一些有趣的錯誤消息。 添加文件只是使用了方法。 您可以在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單元根本不使用它,您不需要在任何地方定義它。

我希望它對你有所幫助。 玩得開心!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM