這篇文章將為大家詳細(xì)講解有關(guān)如何中SpringBoot中添加一個(gè)單元測(cè)試,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),安寧企業(yè)網(wǎng)站建設(shè),安寧品牌網(wǎng)站建設(shè),網(wǎng)站定制,安寧網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,安寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
首先我們來(lái)添加單元測(cè)試所需要的jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
接著我們寫(xiě)了一個(gè)單元測(cè)試的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()); } }
然后我們r(jià)un一下,一個(gè)單元測(cè)試就搞定了。
另外:@RunWith和@SprintBootTest這兩個(gè)注解上都有@Inherited這個(gè)注解,所以我們可以定義一個(gè)單元測(cè)的父類(lèi),然后所有的單元測(cè)試類(lèi)繼承這個(gè)父類(lèi)就行了。如下所示:
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中添加一個(gè)單元測(cè)試就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
本文題目:如何中SpringBoot中添加一個(gè)單元測(cè)試
文章源于:http://www.rwnh.cn/article24/pgscce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、關(guān)鍵詞優(yōu)化、企業(yè)建站、企業(yè)網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)