ServletUnit example for testing JSP

Can someone give me an example of using ServletUnit to test JSP? Do I need to call registerServlet ()? If so, what class name do I pass?

+3
source share
2 answers

You do not need to register a Servlet if you intend to use the default Jasper compiler. However, I need Jasper banks and their dependencies on CLASSPATH. The Maven dependencies that I need to get the base JSP for compilation and rendering were:

<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>

I am stuck in a JDK1.4 project, so you can use newer versions. I have not received the standard taglib yet ...

+2
source

, 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: ApplicationConext.

, ServletRunner Web.xml. , , . , 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 set true. , . . API, .

Get request

 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 , .

, . !

0

Source: https://habr.com/ru/post/1699684/


All Articles