日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

天呐!惊人的Springboot测试.Springboot测试类之@RunWith注解

發(fā)布時(shí)間:2024/3/13 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天呐!惊人的Springboot测试.Springboot测试类之@RunWith注解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Springboot測(cè)試類之@RunWith注解

  • Springboot測(cè)試類之@RunWith注解

Springboot測(cè)試類之@RunWith注解

@runWith注解作用:

  • @RunWith就是一個(gè)運(yùn)行器
  • @RunWith(JUnit4.class)就是指用JUnit4來(lái)運(yùn)行
  • @RunWith(SpringJUnit4ClassRunner.class),讓測(cè)試運(yùn)行于

Spring測(cè)試環(huán)境,以便在測(cè)試開(kāi)始的時(shí)候自動(dòng)創(chuàng)建Spring的應(yīng)用上下文
–@RunWith(Suite.class)的話就是一套測(cè)試集合

引申:
Spring Boot 1.5.2 Junit測(cè)試
使用 Spring 進(jìn)行單元測(cè)試
方法1:【參考文獻(xiàn)】

@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @EnableAutoConfiguration public class BBTestAA {@Autowiredprivate TestRestTemplate testRestTemplate;//Application.class 為SpringBoot的啟動(dòng)入口類,每個(gè)SpringBoot項(xiàng)目大家都會(huì)配置 }

如果pom.xml中有如下代碼,這行@RunWith(SpringRunner.class)就不會(huì)出現(xiàn)SpringRunner,反而有@RunWith(SpringJUnit4ClassRunner.class)

<!--spring-test測(cè)試=--> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version> </dependency> 如果pom.xml中沒(méi)有這段,則@RunWith(SpringRunner.class)不會(huì)報(bào)錯(cuò)。如果有這段:①未注釋<scope>test</scope>會(huì)報(bào)錯(cuò);②注釋<scope>test</scope>不會(huì)報(bào)錯(cuò) <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope> </dependency> 如果pom.xml中沒(méi)有這段,則會(huì)報(bào)錯(cuò)。如果有這段:①未注釋<scope>test</scope>SpringRunner、SpringBootTest無(wú)法引用,會(huì)報(bào)錯(cuò);②注釋<scope>test</scope>不會(huì)報(bào)錯(cuò) <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.5.9.RELEASE</version><scope>test</scope> </dependency>

總結(jié)起來(lái),想要使用

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
pom.xml中應(yīng)該引用這兩個(gè)

<!--spring-test測(cè)試=--><!--<dependency>--><!--<groupId>org.springframework</groupId>--><!--<artifactId>spring-test</artifactId>--><!--<version>4.2.4.RELEASE</version>--><!--</dependency>--><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.5.9.RELEASE</version><!--<scope>test</scope>--></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><!--<scope>test</scope>--></dependency>

方法2:【參考文獻(xiàn)】
如果有test@RunWith報(bào)紅,沒(méi)有test會(huì)引入該類

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope> </dependency>

如果有test@SpringBootTest報(bào)紅,沒(méi)有test會(huì)引入該類

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>1.5.9.RELEASE</version><scope>test</scope> </dependency>

如果是4.2.4.RELEASESpringRunner報(bào)紅,如果4.2.4.RELEASE會(huì)引入該類

org.springframework spring-test 4.2.4.RELEASE 所以最后要正確使用,需引入這些架包 <!--spring-test測(cè)試=--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>1.5.9.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>

2.在IDE中新增JunitTest類

@RunWith(SpringRunner.class) //14.版本之前用的是SpringJUnit4ClassRunner.class @SpringBootTest(classes = Application.class) //1.4版本之前用的是//@SpringApplicationConfiguration(classes = Application.class) public class SystemInfoServiceImplTest {@Autowiredprivate ISystemInfoService systemInfoservice;@Testpublic void add() throws Exception {}@Testpublic void findAll() throws Exception {}}

主要是注解的更改,如果注解用的不對(duì),會(huì)報(bào)各種奇怪的問(wèn)題,例如applicationContext找不到,datasource實(shí)例化失敗等等。

為了支持上面兩個(gè)注解,maven文件中要用對(duì)依賴以及版本,我當(dāng)時(shí)添加SpringRunner.class所在的依賴jar時(shí),由于用了idea的auto-imported,IDE自動(dòng)導(dǎo)入了版本是3.x的,實(shí)際應(yīng)該導(dǎo)入4.x,我一直以為idea導(dǎo)入的是正確的,導(dǎo)致在這上面費(fèi)時(shí)頗多,后來(lái)我手工寫入就解決了。下面是正確的spring boot test的maven依賴。

【參考文獻(xiàn)】

總結(jié)

以上是生活随笔為你收集整理的天呐!惊人的Springboot测试.Springboot测试类之@RunWith注解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。