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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IDEA出现import org.junit.Test飘红解决方案

發(fā)布時間:2025/4/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IDEA出现import org.junit.Test飘红解决方案 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問題描述:
import org.junit.Test;飄紅

問題分析:

Spring Boot新版本默認使用Junit5,pom依賴如下圖,此時不能直接使用import org.junit.Test

如果項目中要使用舊版的Junit4,那么在pom文件中要刪除掉<exclusions>這個對舊版本的支持,同時導入Junit4的相關(guān)依賴,如下:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency> <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope> </dependency>

(以上starter和junit中的<scope>test</scope>都需要刪除,下文會說明原因,復制時請注意)
修改完依賴之后還是會報錯
此時需要打開file-project structure,把Maven: junit:junit:4.12后面的test改成compile,然后點擊apply再點擊ok。如下圖所示


然后我們發(fā)現(xiàn)Test飄紅消失了。
但是新的問題出現(xiàn)了,當輸入完import org.junit.Test;之后立馬消失,如下圖

參考別人的博客:解決方法是刪掉pom.xml文件中junit依賴的scope這一行,這一行的意思是:范圍只能在test目錄下使用,在其他包下使用時不能導入。刪掉改行即可。


但是并沒有成功,還是會消失。

繼續(xù)找bug
原來是auto import里面有兩個方框不要框選


大功告成可以測試

具體測試代碼

package com.example.demo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.*; @SpringBootTest @RunWith(SpringRunner.class)public class GetPersonInfoController {//獲取配置文件中的age@Value("${age}")private int age;//獲取配置文件中的name@Value("${name}")private String name;//該注解表示一個測試方法@Testpublic void getAge(){System.out.println(age);}//該注解表示一個測試方法@Testpublic void getName(){System.out.println(name);} }

參考博客
Spring Boot中進行Junit測試
import org.junit.Test后立馬消失的問題

總結(jié)

以上是生活随笔為你收集整理的IDEA出现import org.junit.Test飘红解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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