Unit testing is a testing of the smallest unit, that is, the method. To isolate dependencies on its modules, stub and mock are generally used.
1. Integration Testing integration testing is a function testing. For most web modules, you need to start web containers for integration testing.
The Maven life cycle includes test and integration test, but the two test codes are not differentiated. You need to resolve the issue of starting web containers and code differentiation by yourself.
2. Set the maven jetty plug-in to start automatically. First, configure the maven jetty plug-in to automatically start in the integration test phase.
<Plugin> <groupid> Org. mortbay. jetty </groupid> <artifactid> Maven-jetty-plugin </artifactid> <version> 6.1.10 </version> <configuration> <stopport> 9966 </stopport> <stopkey> stop -jetty-for-it </stopkey> <contextpath>/sdkauth </contextpath> <scanintervalseconds> 10 </scanintervalseconds> <jettyenvxml >$ {basedir}/src/test/Resources /jetty-env.xml </jettyenvxml> </configuration> <executions> <execution> <ID> Start-J Etty </ID> <! -- Start jetty before integration test --> <phase> pre-integration-test </phase> <goals> <goal> RUN </goal> </goals> <configuration> <daemon> true </daemon> </configuration> </execution> <ID> stop-jetty </ID> <! -- Stop Jetty after integration test ends --> <phase> post-integration-test </phase> <goals> <goal> stop </goal> </goals> </execution> </executions> </plugin>
3. Two Methods of Integration Testing
The unit test and integration test of the test code are divided into two methods:
First, use Maven profile, which is distinguished by profile.
Second, configure the replicdes or/exclueds attributes of the maven surefire plug-in for different lifecycles Based on the lifecycle.
Method 2: Put the unit test code in the src/test/Java connector unit package and the integration test code in the integration package. The specific configuration is as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.9</version> <configuration> <skip>true</skip> </configuration> <executions> <execution> <id>run-integration-test</id> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <skip>false</skip> <includes> <include>**/integration/**/*.java</include> </includes> </configuration> </execution> <execution> <id>run-test</id> <phase>test</phase> <goals> <goal>test</goal> </goals> <configuration> <skip>false</skip> <includes> <include>**/unit/**/*.java</include> </includes> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.0</version> <configuration> <dependentWarExcludes>WEB-INF/lib</dependentWarExcludes> </configuration> </plugin> </plugins>
See http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing
3. Run the integration test command:
mvn integration-test
4. Results:
2012-05-16 15:36:44.293::INFO: Started [email protected]:8080 [INFO] Started Jetty Server [INFO] Starting scanner at interval of 10 seconds. ------------------------------------------------------- T E S T S ------------------------------------------------------- Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.346 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS