iamhuzl

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            1 隨筆 :: 13 文章 :: 21 評論 :: 0 Trackbacks
              單元測試是對最小單元即方法的測試,要隔離對他模塊的依賴,一般采用stub和mock兩種方式
              集成測試是對功能的測試,對于大部分web模塊來說需要啟動web容器,進行集成測試
              maven生命周期中已經包含測試(test)和集成測試(integration-test),但未對兩種測試代碼做區分,需要自己解決啟動web容器和代碼區分問題。
              首先配置maven jetty插件在集成測試階段自動啟動
             
          <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-jetty</id>
                                  <!--集成測試前啟動jetty-->
                                  <phase>pre-integration-test</phase>
                                  <goals>
                                      <goal>run</goal>
                                  </goals>
                                  <configuration>
                                      <daemon>true</daemon>
                                  </configuration>
                              </execution>
                              <execution>
                                  <id>stop-jetty</id>
                                  <!--集成測試結束停止jetty-->
                                  <phase>post-integration-test</phase>
                                  <goals>
                                      <goal>stop</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
          

             然后測試代碼進行單元測試和集成測試區分,一般有兩種方式:
             一是使用maven profile,通過profile區分
             二是根據生命周期,配置maven surefire 插件不同生命周期的includes或/exclueds屬性
             我使用的方式二,在src/test/java目中把單元測試代碼放在unit包,集成測試代碼放在integration包,具體配置如下:
            
          <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>
          

             可以參考http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing
              運行集成測試命令 : mvn integration-test
          引用

          2012-05-16 15:36:44.293::INFO:  Started SelectChannelConnector@0.0.0.0: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



          已有 0 人發表留言,猛擊->>這里<<-參與討論


          ITeye推薦



          posted on 2012-05-16 15:40 溫水青蛙 閱讀(2993) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 庆元县| 义乌市| 宜川县| 静宁县| 保德县| 柯坪县| 修武县| 泾川县| 阜城县| 兴国县| 石阡县| 会宁县| 维西| 木里| 富民县| 祥云县| 河源市| 宜兴市| 甘孜| 益阳市| 晋江市| 三河市| 和硕县| 信宜市| 永吉县| 白银市| 商城县| 外汇| 新郑市| 石泉县| 宿松县| 桂阳县| 新营市| 顺昌县| 新化县| 揭东县| 丹江口市| 丹寨县| 疏勒县| 元阳县| 聂荣县|