मैंने पोस्ट किए गए उत्तरों को देखा और सोचा कि मैं एक और पूरा समाधान पोस्ट करूंगा जो वास्तव में एम्बेडेड ग्लासफ़िश और उसके अपाचे मैवेन प्लगइन का उपयोग करके परीक्षण कैसे करें।
मैं अपने ब्लॉग Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x पर पूरा प्रक्रिया में तेजी लिखा था और यहाँ Bitbucket पर डाउनलोड के लिए पूरा परियोजना रखा: image-servlet
मैं JSP/JSF टैग के लिए एक छवि सर्वलेट पर एक और पोस्ट में बस से पहले मैं इस देखा देख रहा था सवाल।इसलिए मैंने इस पोस्ट के लिए एक पूर्ण इकाई परीक्षण संस्करण के साथ दूसरे पोस्ट से उपयोग किए गए समाधान को जोड़ा।
कैसे टेस्ट
अपाचे Maven एक अच्छी तरह से परिभाषित जीवन चक्र कि test
भी शामिल है। मैं अपने समाधान को लागू करने के लिए integration-test
नामक एक और जीवन चक्र के साथ इसका उपयोग करूंगा।
- surefire प्लगइन में मानक जीवन चक्र इकाई परीक्षण अक्षम करें।
- surefire-plugin
- के निष्पादन के हिस्से के रूप में
integration-test
जोड़ें पीओएम में ग्लासफ़िश मेवेन प्लगइन जोड़ें।
integration-test
जीवन चक्र के दौरान निष्पादित करने के लिए ग्लासफ़िश कॉन्फ़िगर करें।
- रन यूनिट परीक्षण (एकीकरण परीक्षण)।
ग्लासफिश प्लगइन
<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>
अचूक प्लगइन
जोड़ें/<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
नीचे दिए गए उदाहरण की तरह एकीकरण परीक्षणों जोड़ें।
@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();
}
मैं अपने ब्लॉग Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x पर पूरा प्रक्रिया में तेजी लिखा था और यहाँ Bitbucket पर डाउनलोड के लिए पूरा परियोजना रखा: image-servlet
आप कोई प्रश्न हैं, एक टिप्पणी छोड़ कृपया। मुझे लगता है कि यह आपके लिए सर्वलेट के लिए योजना बना रहे किसी भी परीक्षण के आधार के रूप में उपयोग करने के लिए एक संपूर्ण उदाहरण है।
स्रोत
2013-01-06 01:54:00
[यूनिट परीक्षण सर्वलेट्स] (के संभावित डुप्लिकेट http://stackoverflow.com/questions/53532/unit-testing- servlets) – Raedwald