雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

Java

當(dāng)前位置:主頁(yè) > 軟件編程 > Java >

springboot單元測(cè)試兩種方法實(shí)例詳解

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Java|點(diǎn)擊:

這篇文章主要介紹了springboot單元測(cè)試兩種方法實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

springboot的單元測(cè)試,這里介紹兩種方式,一種是在測(cè)試類(lèi)中添加注解;另一種是在代碼中啟動(dòng)項(xiàng)目的main方法中繼承接口(也可以寫(xiě)在其他方法中)。

  如 對(duì)查看數(shù)據(jù)庫(kù)的連接池信息 進(jìn)行單元測(cè)試

1. 在類(lèi)上使用注解:

  @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í)行該功能,只需將測(cè)試的內(nèi)容寫(xiě)在其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);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Springboot測(cè)試類(lèi)沒(méi)有bean注入問(wèn)題解析

欄    目:Java

下一篇:Spring @Transactional注解失效解決方案

本文標(biāo)題:springboot單元測(cè)試兩種方法實(shí)例詳解

本文地址:http://www.jygsgssxh.com/a1/Java/8896.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有