SpringMVC+MockMvc+dbUnit+cobertura構建測試框架

本文基於SpringMVC搭建測試框架
一般狀況咱們能夠藉助easyMock及powerMock進行單元測試,但有時咱們但願進行集成測試,能夠經過發送http請求,測試某功能的完整性。
通常狀況咱們能夠經過MockMvc模擬post或get請求,完成測試。可是當碰到delete或update進行測試時,容易對數據庫形成污染,這時咱們能夠藉助dbunit,對數據庫進行測試數據的準備,測試完成後對事務進行回滾,方便下次測試。
1. maven集成測試組件
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>
<!--數據庫單元測試:消除單元測試對數據庫的污染 start-->
<dependency>
	<groupId>com.github.springtestdbunit</groupId>
	<artifactId>spring-test-dbunit</artifactId>
	<version>1.1.0</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.dbunit</groupId>
	<artifactId>dbunit</artifactId>
	<version>2.4.9</version>
	<scope>test</scope>
</dependency>
<!--數據庫單元測試:消除單元測試對數據庫的污染 end-->
複製代碼
2. 定義測試基類,包括SpringMVC框架的集成,junit集成
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@PropertySource("classpath:common.properties")
@TestPropertySource("classpath:common.properties")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
        ServletTestExecutionListener.class,
        DbUnitTestExecutionListener.class}) //@1
@ContextConfiguration(
        {"classpath*:/spring-context.xml", "classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)//@2
@Transactional 
public abstract class BaseSpringJUnitTest {

    public static Logger logger = LoggerFactory.getLogger(BaseSpringJUnitTest.class);

    @Autowired
    private UserInfoService userInfoService;


    private static boolean inited = false;

    /**
     * junit模擬用戶名
     */
    private final static String USER_NAME = "admin";

    /**
     * junit模擬驗證碼
     */
    private final static String VALIDATE_CODE = "1234";

    /**
     * junit模擬密碼
     */
    private final static String PASSWORD = "Admin123456";

    public static String token = "";

    protected MockMvc mockMvc; //@3

    @Before //@4
    public void setUp() throws Exception {
        if (!inited) {
            String code = userInfoService.getValidateKey().get("validateKey").toString();
            RedisUtils.set("validCode:" + code, VALIDATE_CODE);
            UserInfoRequestParams params = new UserInfoRequestParams();
            params.setLoginName(USER_NAME);
            params.setPassword(MD5Util.encoderHexByMd5(PASSWORD));
            params.setValidateKey(code);
            params.setValidateCode(VALIDATE_CODE);

            JSONOutputObject result = userInfoService.webLogin(params);
            token = result.get("token").toString();
            TestCase.assertEquals(RsmsConstant.RESULT_SUCCESS_CODE, result.get(RsmsConstant.RESULT_CODE));
            inited = true;
        }
    }

}
複製代碼

@1:ServletTestExecutionListener 用於設置spring web框架啓動時的RequestContextHolder本地線程變量。咱們的項目比較特殊,在service層中是經過RequestContextHolder獲取httpRequest對象(並不是經過controller透傳),若是不設置,則在service中或者切面中沒法獲取到request對象html

@2:添加事務回滾,避免對數據庫的污染git

@3:定義MockMvc對象,模擬客戶端的http請求github

@4:初始化登陸信息,根據自身須要設置,有些集成測試場景可能須要登陸信息web

3.編寫測試類
public class UserInfoControllerTest extends BaseSpringJUnitTest {

    @Test
    @DatabaseSetup(type = DatabaseOperation.INSERT, value = {"/data/user-info.xml"})//@1
    public void testDeleteUsers() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        //@2
        String responseString = mockMvc.perform(post("/user/deleteUsers")
                .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                .content("{\"idList\":10007}")
                .header("Current-Menu-Id", "102100")
                .header("Menu-Id", "102100")
                .accept(MediaType.ALL_VALUE)
                .header("token", token)
                .header("User-Agent", "Windows NT")
        ).andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();
        JSONObject jsonObject = JSONObject.parseObject(responseString);
        Assert.assertEquals(jsonObject.get("resultCode"), "0000");
    }
}
複製代碼

@1:準備測試數據,由於在測試時,若是不本身準備數據,依賴數據庫數據,那麼數據庫數據有可能被其餘人誤刪或者數據庫作了遷移或更新以後,咱們的測試用例將沒法跑通。spring

@2:經過mockMvc發送http請求,並解析請求結果,判斷測試結果的正確性數據庫

4.準備測試數據(resource/data/user_info.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
    <t_user_info id="10007" login_name="admin_junit" password="" salt="" user_name="admin_junit" user_name_spell=""
              user_name_initial="" eid="" cellphone="" company_id="200" org_id="200" position=""/>
    <t_user_role id="1000" user_id="10007" role_id="1" />
</dataset>
複製代碼
5.jenkins集成,並統計單元測試覆蓋率
<!-- 統計junit覆蓋率 -->
<plugin>
	<groupId>org.jacoco</groupId>
	<artifactId>jacoco-maven-plugin</artifactId>
	<version>0.8.1</version>
	<executions>
		<execution>
			<goals>
				<goal>prepare-agent</goal>
			</goals>
		</execution>
		<execution>
			<id>report</id>
			<phase>prepare-package</phase>
			<goals>
				<goal>report</goal>
			</goals>
		</execution>
	</executions>
</plugin>
複製代碼
jenkins添加普通的maven構建任務,設置好git地址

插件管理中安裝jacoco插件 build項設置以下 Root POM pom.xml Goals and options test -Dmaven.repo.local=/opt/repository 構建後操做添加:Record JaCoCo coverage reportjson

統計結果效果以下

單元測試覆蓋率統計

6.配置郵件發送

郵件發送本人是集成了cobertura(和jacoco共同樣,都是用於覆蓋率的統計)spring-mvc

pom集成bash

<!-- cobertura統計junit覆蓋率 -->
          <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                </configuration>
            </plugin>

複製代碼

jenkins全局配置:jenkins->系統管理->系統設置 系統管理員地址:*********@qq.com Extended E-mail Notification中配置以下 SMTP:smtp.qq.com 後綴@qq.com 點開高級 使用SMTP認證 用戶名:*********@qq.com 密碼:qq給的受權碼(非郵箱的登陸密碼,受權碼的獲取:登陸QQ郵箱:設置-SMTP設置-開啓,須要發送短信,發送短信後,頁面會顯示受權碼)服務器

jenkins構建任務配置郵件發送 構建後操做:增長Editable Email Notification,點開高級,必定要設置triggers,不然沒法觸發

配置coberture插件.png
郵件發送.png
郵件發送我用的是jelly模板,使用的時候只須要填寫模板文件名稱(.jelly後綴不須要),模板文件放在jenkins服務器上,具體模板代碼見附件一,見下圖
郵件模板位置.png
配置trigger.png

7.郵件發送最終效果

郵件主要內容:構建狀態、覆蓋率總體狀況、包覆蓋率狀況、UT趨勢、變動集等等

郵件效果.png

附件一:jelly郵件模板
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
  <html>
    <head>
      <title>${project.name}</title>
      <style>
        body table, td, th, p, h1, h2 {
        margin:0;
        font:normal normal
        100% Georgia, Serif;
        background-color: #ffffff;
        }
        h1, h2 {
        border-bottom:dotted 1px #999999;
        padding:5px;
        margin-top:10px;
        margin-bottom:10px;
        color: #000000;
        font: normal bold 130%
        Georgia,Serif;
        background-color:#f0f0f0;
        }
        tr.gray {
        background-color:#f0f0f0;
        }
        h2 {
        padding:5px;
        margin-top:5px;
        margin-bottom:5px;
        font: italic bold 110% Georgia,Serif;
        }
        .bg2 {
        color:black;
        background-color:#E0E0E0;
        font-size:110%
        }
        th {
        font-weight: bold;
        }
        tr, td, th {
        padding:2px;
        }
        td.test_passed {
        color:blue;
        }
        td.test_failed {
        color:red;
        }
        td.center {
          text-align: center;
        }
        td.test_skipped {
        color:grey;
        }
        .console {
        font: normal normal 90% Courier New,
        monotype;
        padding:0px;
        margin:0px;
        }
        div.content, div.header {
        background: #ffffff;
        border: dotted
        1px #666;
        margin: 2px;
        content:
        2px;
        padding: 2px;
        }
        table.border, th.border, td.border {
        border:
        1px solid black;
        border-collapse:collapse;
        }
      </style>
    </head>
    <body>
      <div class="header">
        <j:set var="spc" value="&amp;nbsp;&amp;nbsp;" />
        <!-- GENERAL INFO -->
        <table>
          <tr class="gray">
            <td align="right">
              <j:choose>
                <j:when test="${build.result=='SUCCESS'}">
                  <img src="${rooturl}static/e59dfe28/images/32x32/blue.gif" />
                </j:when>
                <j:when test="${build.result=='FAILURE'}">
                  <img src="${rooturl}static/e59dfe28/images/32x32/red.gif" />
                </j:when>
                <j:otherwise>
                  <img
                    src="${rooturl}static/e59dfe28/images/32x32/yellow.gif" />
                </j:otherwise>
              </j:choose>
            </td>
            <td valign="center">
              <b style="font-size: 200%;">BUILD ${build.result}</b>
            </td>
          </tr>
          <tr>
            <td>構建地址</td>
            <td>
              <a href="${rooturl}${build.url}">${rooturl}${build.url}</a>
            </td>
          </tr>
          <tr>
            <td>項  目:</td>
            <td>${project.name}</td>
          </tr>
          <tr>
            <td>構建日期:</td>
            <td>${it.timestampString}</td>
          </tr>
          <tr>
            <td>構建時長:</td>
            <td>${build.durationString}</td>
          </tr>
          <tr>
            <td>Build cause:</td>
            <td>
              <j:forEach var="cause" items="${build.causes}">${cause.shortDescription}
              </j:forEach>
            </td>
          </tr>
          <tr>
            <td>Build description:</td>
            <td>${build.description}</td>
          </tr>
          <tr>
            <td>Built on:</td>
            <td>
              <j:choose>
                <j:when test="${build.builtOnStr!=''}">${build.builtOnStr}</j:when>
                <j:otherwise>master</j:otherwise>
              </j:choose>
            </td>
          </tr>
        </table>
      </div>
	  
	  <!-- COBERTURA TEMPLATE -->
      <j:set var="coberturaAction" value="${it.coberturaAction}" />
      <j:if test="${coberturaAction!=null}">
        <div class="content">
          <j:set var="coberturaResult" value="${coberturaAction.result}" />
          <j:if test="${coberturaResult!=null}">
		    
            <a href="${rooturl}${build.url}/cobertura">
              <h1>Cobertura報告</h1>
            </a>
			<h2>趨勢</h2>
			<img src="http://*.*.*.*:****/${build.url}../cobertura/graph" width="600" height="200px" />
            <h2>項目覆蓋率彙總</h2>
            <table class="border">
              <tr>
                <th class="border">Name</th>
                <j:forEach var="metric" items="${coberturaResult.metrics}">
                  <th class="border">${metric.name}</th>
                </j:forEach>
              </tr>
              <tr>
                <td class="border">${coberturaResult.name}</td>
                <j:forEach var="metric" items="${coberturaResult.metrics}">
				  <!--
                  <td class="border"
                    data="${coberturaResult.getCoverage(metric).percentageFloat}">${coberturaResult.getCoverage(metric).percentage}%
                    (${coberturaResult.getCoverage(metric)})
                  </td>
				  -->
				  <td class="border">
					  <div style="background-color:#ff9090;width:100px;height:20px;">
						<div style="background-color:#80ff80;width: ${coberturaResult.getCoverage(metric).percentage}px;height:20px;">
							<span style="text-align:center;position:absolute;width:100px;" vertical-align="middle">
								${coberturaResult.getCoverage(metric).percentage}%(${coberturaResult.getCoverage(metric)})
							</span>
						</div>
					  </div>
				  </td>
                </j:forEach>
              </tr>
            </table>
			
            <j:if test="${coberturaResult.sourceCodeLevel}">
              <h2>Source</h2>
              <j:choose>
                <j:when test="${coberturaResult.sourceFileAvailable}">
                  <div style="overflow-x:scroll;">
                    <table class="source">
                      <thead>
                        <tr>
                          <th colspan="3">${coberturaResult.relativeSourcePath}
                          </th>
                        </tr>
                      </thead>
                      ${coberturaResult.sourceFileContent}
                    </table>
                  </div>
                </j:when>
                <j:otherwise>
                  <p>
                    <i>Source code is unavailable</i>
                  </p>
                </j:otherwise>
              </j:choose>
            </j:if>
			
            <j:forEach var="element" items="${coberturaResult.childElements}">
              <j:set var="childMetrics"
                value="${coberturaResult.getChildMetrics(element)}" />
              <h2>Coverage Breakdown by ${element.displayName}</h2>
              <table class="border">
                <tr>
                  <th class="border">Name</th>
                  <j:forEach var="metric" items="${childMetrics}">
                    <th class="border">${metric.name}</th>
                  </j:forEach>
                </tr>
                <j:forEach var="c" items="${coberturaResult.children}">
                  <j:set var="child" value="${coberturaResult.getChild(c)}" />
                  <tr>
                    <td class="border">
                      ${child.xmlTransform(child.name)}
                    </td>
                    <j:forEach var="metric" items="${childMetrics}">
                      <j:set var="childResult" value="${child.getCoverage(metric)}" />
                      <j:choose>
                        <j:when test="${childResult!=null}">
						  <!--
                          <td class="border" data="${childResult.percentageFloat}">${childResult.percentage}%
                            (${childResult})
                          </td>
						  -->
						  <td class="border" data="${childResult.percentageFloat}">
							  <div style="background-color:#ff9090;width:100px;height:20px;">
								<div style="background-color:#80ff80;width: ${childResult.percentage}px;height:20px;">
									<span style="text-align:center;position:absolute;width:100px;">
										${childResult.percentage}%(${childResult})
									</span>
								</div>
							  </div>
						  </td>
                        </j:when>
                        <j:otherwise>
                          <td class="border" data="101">N/A</td>
                        </j:otherwise>
                      </j:choose>
                    </j:forEach>
                  </tr>
                </j:forEach>
              </table>
            </j:forEach>
          </j:if>
          <br />
        </div>
      </j:if>
	  
	  <!-- 覆蓋率趨勢統計 -->
	  <!--
      <j:set var="coberturaAction" value="${it.coberturaAction}" />
      <j:if test="${coberturaAction!=null}">
        <div class="content">
          <j:set var="coberturaResult" value="${coberturaAction.result}" />
          <j:if test="${coberturaResult!=null}">
            <a href="${rooturl}${build.url}/cobertura">
              <h1>覆蓋率趨勢</h1>
            </a>
			
			<j:forEach var="metric" items="${coberturaResult.metrics}">
				<table style="padding: 0 10px; width:480px;">
					<tbody>
					<tr>
						<th align="left">${metric.name}</th>
						<td align="right"
							data="${coberturaResult.getCoverage(metric).percentageFloat}">${coberturaResult.getCoverage(metric).percentage}%
							(${coberturaResult.getCoverage(metric)})
						</td>
					</tr>
					</tbody>
				</table>
				<table style="height: 3px; padding: 0 10px; width:480px;">
					<tbody>
					<tr>
						<td width="${coberturaResult.getCoverage(metric).percentage}%" height="20" style="background-color:#bfb;">
						</td>
						<td style="background-color:#fdd;">
						</td>
					</tr>
					</tbody>
				</table>
			</j:forEach>

			
          </j:if>
          <br />
        </div>
      </j:if>
	  -->
	  
      <!-- HEALTH TEMPLATE -->
      <div class="content">
        <j:set var="healthIconSize" value="16x16" />
        <j:set var="healthReports" value="${project.buildHealthReports}" />
        <j:if test="${healthReports!=null}">
          <h1>健康報告</h1>
          <table>
            <tr>
              <th>W</th>
              <th>描述</th>
              <th>分數</th>
            </tr>
            <j:forEach var="healthReport" items="${healthReports}">
              <tr>
                <td>
                  <img
                    src="${rooturl}${healthReport.getIconUrl(healthIconSize)}" />
                </td>
                <td>${healthReport.description}</td>
                <td>${healthReport.score}</td>
              </tr>
            </j:forEach>
          </table>
          <br />
        </j:if>
      </div>

      <!-- CHANGE SET -->
      <div class="content">
        <j:set var="changeSet" value="${build.changeSet}" />
        <j:if test="${changeSet!=null}">
          <j:set var="hadChanges" value="false" />
          <a href="${rooturl}${build.url}/changes">
            <h1>變動集</h1>
          </a>
          <j:forEach var="cs" items="${changeSet.logs}"
            varStatus="loop">
            <j:set var="hadChanges" value="true" />
            <h2>${cs.msgAnnotated}</h2>
            <p>
              by
              <em>${cs.author}</em>
            </p>
            <table>
              <j:forEach var="p" items="${cs.affectedFiles}">
                <tr>
                  <td width="10%">${spc}${p.editType.name}</td>
                  <td>
                    <tt>${p.path}</tt>
                  </td>
                </tr>
              </j:forEach>
            </table>
          </j:forEach>
          <j:if test="${!hadChanges}">
            <p>無</p>
          </j:if>
          <br />
        </j:if>
      </div>

      <!-- ARTIFACTS -->
      <j:set var="artifacts" value="${build.artifacts}" />
      <j:if test="${artifacts!=null and artifacts.size()&gt;0}">
        <div class="content">
          <h1>Build Artifacts</h1>
          <ul>
            <j:forEach var="f" items="${artifacts}">
              <li>
                <a href="${rooturl}${build.url}artifact/${f}">${f}</a>
              </li>
            </j:forEach>
          </ul>
        </div>
      </j:if>

      <!-- MAVEN ARTIFACTS -->
      <j:set var="mbuilds" value="${build.moduleBuilds}" />
      <j:if test="${mbuilds!=null}">
        <div class="content">
          <h1>Build Artifacts</h1>
          <j:forEach var="m" items="${mbuilds}">
            <h2>${m.key.displayName}</h2>
            <j:forEach var="mvnbld" items="${m.value}">
              <j:set var="artifacts" value="${mvnbld.artifacts}" />
              <j:if test="${artifacts!=null and artifacts.size()&gt;0}">
                <ul>
                  <j:forEach var="f" items="${artifacts}">
                    <li>
                      <a href="${rooturl}${mvnbld.url}artifact/${f}">${f}</a>
                    </li>
                  </j:forEach>
                </ul>
              </j:if>
            </j:forEach>
          </j:forEach>
          <br />
        </div>
      </j:if>
      <!-- JUnit TEMPLATE -->
      <j:set var="junitResultList" value="${it.JUnitTestResult}" />
      <j:if test="${junitResultList.isEmpty()!=true}">
        <div class="content">
          <a href="${rooturl}${build.url}/testReport">
            <h1>單元測試</h1>
          </a>
          <table class="border">
            <tr>
              <th class="border">包路徑</th>
              <th class="border">失敗</th>
              <th class="border">經過</th>
              <th class="border">跳過</th>
              <th class="border">總計</th>
            </tr>
            <j:forEach var="junitResult" items="${it.JUnitTestResult}">
              <j:forEach var="packageResult" items="${junitResult.getChildren()}">
                <tr>
                  <td class="border">
                    <tt>${packageResult.getName()}</tt>
                  </td>
                  <td class="border test_failed">${packageResult.getFailCount()}</td>
                  <td class="border test_passed">${packageResult.getPassCount()}</td>
                  <td class="border test_skipped">${packageResult.getSkipCount()}</td>
                  <td class="border">
                    <b>${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()}
                    </b>
                  </td>
                </tr>
                <j:forEach var="failed_test"
                  items="${packageResult.getFailedTests()}">
                  <tr>
                    <td class="test_failed" colspan="5">
                      <tt>${failed_test.getFullName()}</tt>
                    </td>
                  </tr>
                </j:forEach>
              </j:forEach>
            </j:forEach>
          </table>
          <br />
        </div>
      </j:if>


      <!-- Static Analysis -->
      <j:set var="actions" value="${it.staticAnalysisActions}" />
      <j:if test="${!actions.isEmpty()}">
        <div class="content">
          <h1>Static Analysis Results</h1>
          <table>
            <tr>
              <th></th>
              <th>Name</th>
              <th>Result</th>
              <th>Total</th>
              <th>High</th>
              <th>Normal</th>
              <th>Low</th>
            </tr>
            <j:forEach var="action" items="${actions}">
              <tr>
                <td>
                  <img src="${rooturl}${action.smallImageName}" />
                </td>
                <td>
                  <a href="${rooturl}${build.url}/${action.urlName}">${action.displayName}</a> 
                </td>
                <td class="center">
                  <j:choose>
                    <j:when test="${action.result.pluginResult=='SUCCESS'}">
                      <img src="${rooturl}static/e59dfe28/images/16x16/blue.gif" />
                    </j:when>
                    <j:when test="${action.result.pluginResult=='FAILURE'}">
                      <img src="${rooturl}static/e59dfe28/images/16x16/red.gif" />
                    </j:when>
                    <j:otherwise>
                      <img src="${rooturl}static/e59dfe28/images/16x16/yellow.gif" />
                    </j:otherwise>
                  </j:choose>
                </td>
                <td class="center">${action.result.numberOfAnnotations} </td>
                <td class="center">${action.result.getNumberOfAnnotations('HIGH')} </td>
                <td class="center">${action.result.getNumberOfAnnotations('NORMAL')} </td>
                <td class="center">${action.result.getNumberOfAnnotations('LOW')} </td>
              </tr>
            </j:forEach>
          </table>
        </div>
      </j:if>
    </body>
  </html>
</j:jelly>

複製代碼
相關文章
相關標籤/搜索