Przyjrzałem się opublikowanym odpowiedziom i pomyślałem, że zamieściłbym bardziej kompletne rozwiązanie, które faktycznie demonstruje, jak wykonać testowanie za pomocą wbudowanego GlassFish i jego wtyczki Apache Maven.
pisałem cały proces się na moim blogu Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x i umieścił kompletny projekt do pobrania na Bitbucket tutaj: image-servlet
szukałem na inne stanowisko w sprawie serwletu obrazu dla JSP/JSF tagów tuż przed Widziałem to pytanie.Więc połączyłem rozwiązanie, którego użyłem z innego postu, z kompletną wersją testowaną dla tego posta.
Jak testować
Apache Maven ma dobrze zdefiniowany cykl, który zawiera test
. Użyję tego razem z innym cyklem życia o nazwie integration-test
do wdrożenia mojego rozwiązania.
- Wyłącz standardowe testowanie jednostki cyklu życia w pluginie "surefire".
- Dodaj
integration-test
jako część egzekucji wtyczki surefire
- Dodaj wtyczkę GlassFish Maven do POM.
- Konfigurowanie GlassFish do wykonania w cyklu życia
integration-test
.
- Wykonaj testy jednostkowe (testy integracyjne).
GlassFish Plugin
Dodaj ten plugin jako część <build>
.
<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<!-- This sets the path to use the war file we have built in the target directory -->
<app>target/${project.build.finalName}</app>
<port>8080</port>
<!-- This sets the context root, e.g. http://localhost:8080/test/ -->
<contextRoot>test</contextRoot>
<!-- This deletes the temporary files during GlassFish shutdown. -->
<autoDelete>true</autoDelete>
</configuration>
<executions>
<execution>
<id>start</id>
<!-- We implement the integration testing by setting up our GlassFish instance to start and deploy our application. -->
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
<goal>deploy</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<!-- After integration testing we undeploy the application and shutdown GlassFish gracefully. -->
<phase>post-integration-test</phase>
<goals>
<goal>undeploy</goal>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
Surefire Plugin
Dodaj/modyfikować wtyczki jako część <build>
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<!-- We are skipping the default test lifecycle and will test later during integration-test -->
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<!-- During the integration test we will execute surefire:test -->
<goal>test</goal>
</goals>
<configuration>
<!-- This enables the tests which were disabled previously. -->
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
HtmlUnit
Dodaj testy integracyjne jak na poniższym przykładzie.
@Test
public void badRequest() throws IOException {
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setPrintContentOnFailingStatusCode(false);
final HtmlPage page = webClient.getPage("http://localhost:8080/test/images/");
final WebResponse response = page.getWebResponse();
assertEquals(400, response.getStatusCode());
assertEquals("An image name is required.", response.getStatusMessage());
webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);
webClient.getOptions().setPrintContentOnFailingStatusCode(true);
webClient.closeAllWindows();
}
pisałem cały proces się na moim blogu Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x i umieścił kompletny projekt do pobrania na Bitbucket tutaj: image-servlet
Jeśli masz jakieś pytania, proszę zostawić komentarz. Myślę, że jest to jeden kompletny przykład, który można wykorzystać jako podstawę każdego testowania, które planujecie dla serwletów.
możliwe duplikat [Urządzenie badające serwletów] (http://stackoverflow.com/questions/53532/unit-testing- aplety) – Raedwald