成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何中SpringBoot中添加一個單元測試

這篇文章將為大家詳細講解有關(guān)如何中SpringBoot中添加一個單元測試,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,安寧企業(yè)網(wǎng)站建設,安寧品牌網(wǎng)站建設,網(wǎng)站定制,安寧網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,安寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

首先我們來添加單元測試所需要的jar

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-test</artifactId> 
</dependency>

接著我們寫了一個單元測試的demo

package com.zkn.learnspringboot.service.test; 
 
import com.zkn.learnspringboot.FirstExample; 
import com.zkn.learnspringboot.service.PersonService; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
 
/** 
 * Created by wb-zhangkenan on 2016/11/18. 
 */ 
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes = FirstExample.class) 
public class TestService extends BaseTestService{ 
  @Autowired 
  private PersonService personService; 
  @Test 
  public void testSys() { 
    System.out.println(personService.getPersonDomain().toString()); 
  } 
 
}

然后我們run一下,一個單元測試就搞定了。

另外:@RunWith和@SprintBootTest這兩個注解上都有@Inherited這個注解,所以我們可以定義一個單元測的父類,然后所有的單元測試類繼承這個父類就行了。如下所示:

package com.zkn.learnspringboot.service.test; 
 
import com.zkn.learnspringboot.FirstExample; 
import org.junit.runner.RunWith; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
 
/** 
 * Created by zkn on 2016/11/20. 
 */ 
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes = FirstExample.class) 
public class BaseTestService { 
 
}
package com.zkn.learnspringboot.service.test; 
 
import com.zkn.learnspringboot.service.PersonService; 
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
 
/** 
 * Created by wb-zhangkenan on 2016/11/18. 
 */ 
 
public class TestService extends BaseTestService{ 
  @Autowired 
  private PersonService personService; 
  @Test 
  public void testSys() { 
    System.out.println(personService.getPersonDomain().toString()); 
  } 
 
}

如果你用的SpringBoot是1.4.0之前的話,所用的注解稍有不同。你需要把@SpringBootTest注解換成@SpringApplicationConfiguration和@WebAppConfiguration。

關(guān)于如何中SpringBoot中添加一個單元測試就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

本文題目:如何中SpringBoot中添加一個單元測試
文章源于:http://jinyejixie.com/article24/pgscce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT關(guān)鍵詞優(yōu)化、企業(yè)建站、企業(yè)網(wǎng)站制作、網(wǎng)站建設、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設
栾城县| 香格里拉县| 灵台县| 梅河口市| 肥东县| 左贡县| 毕节市| 辉县市| 揭东县| 台州市| 临汾市| 连州市| 上饶市| 南涧| 双流县| 宿州市| 页游| 竹溪县| 芒康县| 大同市| 延津县| 罗甸县| 密山市| 连平县| 金川县| 崇文区| 佛学| 东阳市| 长兴县| 焦作市| 安陆市| 新干县| 呼玛县| 祁连县| 香格里拉县| 武冈市| 临猗县| 策勒县| 上林县| 商丘市| 绥中县|