springBoot測試類報Failed to resolve org.junit...錯誤


解決idea寫spring boot運行測試類時出現“Failed to resolve org...”的問題

今天在學習spring Boot時,運行測試類運行時長時間下載文件,響應后卻出現以下錯誤:

方法一:修改鏡像源

嘗試將maven的配置文件改為阿里雲的鏡像源。路徑:你的安裝目錄/conf/settings.xml,找到mirrors標簽,在標簽內添加以下代碼保存退出即可。

<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>

方法二:添加配置依賴

若修改了鏡像源,還是出現無法運行問題,在idea打開項目,在pom.xml文件中的dependencies標簽中添加以下代碼:

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

添加完畢后,在測試類中添加@RunWith(SpringRunner.class)並導入相應的包,需要注意的是,@Test的包是導入org.junit.Test而不是 org.junit.jupiter.api.Test,參考的測試代碼如下所示:

import com.itheima.chapter02.domain.Person;
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.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter02ApplicationTests {
    @Autowired
    private Person person;
    @Test
    public void contextLoads() {
        System.out.println(person);
    }
}

需要注意的是,需要將類和方法設置為public才可以運行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM