springboot單元測試兩種方法實(shí)例詳解
這篇文章主要介紹了springboot單元測試兩種方法實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
springboot的單元測試,這里介紹兩種方式,一種是在測試類中添加注解;另一種是在代碼中啟動(dòng)項(xiàng)目的main方法中繼承接口(也可以寫在其他方法中)。
如 對查看數(shù)據(jù)庫的連接池信息 進(jìn)行單元測試
1. 在類上使用注解:
@RunWith(SpringRunner.class)
@SpringBootTest
@RunWith(SpringRunner.class) @SpringBootTest public class RobotsApplicationTests { @Autowired DataSource dataSource; @Test public void test(){ System.out.println(dataSource.getClass()); } }
2. 繼承CommandLineRunner接口
CommandLineRunner:表示在項(xiàng)目啟動(dòng)完成后 會(huì)執(zhí)行該功能,只需將測試的內(nèi)容寫在其run()方法中,如:
@SpringBootApplication @EnableScheduling @ComponentScan(basePackages={"com.cmit.hall.plat","com.cmit.hall.pub"}) @ServletComponentScan(value= {"com.cmit.hall.pub.interceptor","com.cmit.hall.plat.config","com.cmit.hall.pub.session"}) @EnableRedisHttpSession(maxInactiveIntervalInSeconds=1800) public class PlatApp implements CommandLineRunner { @Autowired DataSource dataSource; public static void main(String[] args) { SpringApplication.run(PlatApp.class, args); } @Override public void run(String... args) throws Exception { System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作<<<<<<<<<<<<<"); System.out.println("DATASOURCE = " + dataSource); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:Java
下一篇:Spring @Transactional注解失效解決方案
本文標(biāo)題:springboot單元測試兩種方法實(shí)例詳解
本文地址:http://mengdiqiu.com.cn/a1/Java/8896.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10springboot集成fastDfs過程代碼實(shí)例
- 01-10SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解
- 01-10springboot 配置DRUID數(shù)據(jù)源的方法實(shí)例分析
- 01-10springboot2.0使用Hikari連接池的方法(替換druid)
- 01-10Springboot測試類沒有bean注入問題解析


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置