![](/img/trans.png)
[英]Gradle project builds successfully but IntelliJ cannot resolve references
[英]Intellij Gradle project cannot resolve assertEquals with junit 4.11 as the testCompile dep
我正在嘗試在最新版本的 Intellij IDEA (13.0.2) 中設置一個簡單的 gradle 項目。
除了 JUnit 4,我沒有其他依賴項,我的 build.gradle 文件如下所示:
apply plugin: 'java'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
我正在嘗試在我的主類測試套件中使用 assertEquals,但 Intellij 為我提供了以下兩個使用實例的“無法解析方法 assertEquals(int, int)”:
package ag.kge;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert.*;
public class MainTest {
Main testMain1;
Main testMain2;
@Before
public void setUp(){
testMain1 = new Main("9999");
testMain2 = new Main("args");
}
@Test
public void testGetPort() throws Exception {
assertEquals (testMain1.getPort(), 9999);
assertEquals (testMain2.getPort(), 5000);
}
@Test
public void testStartThreads() throws Exception {
}
}
此外,Intellij 表示未使用 import org.junit.Assert.*。
如果有人知道我為什么會遇到這個問題,我將非常感謝您的幫助。 謝謝。
import org.junit.Assert.*;
應該
import static org.junit.Assert.*;
我遇到了同樣的問題並通過將 assertEquals 更改為
Assert.assertEquals
和
import org.junit.Assert;
希望這對下線的人有所幫助。
使用 IntelliJ 2019.2.4 和 start.sping.io 默認設置...
import static org.junit.jupiter.api.Assertions.assertEquals;
但不是
Assert.assertEquals(expected, actual);
用
assertEquals(expected, actual);
所以我只是做了 intelliJ 並遇到了同樣的問題,我的自動修復按選項 + 輸入按鈕
它導入了這個:
import static org.junit.jupiter.api.Assertions.assertEquals;
然后為我的測試自動修復這個
@Test
void UserService() {
//testing login
UserService service = new UserService();
UserServiceBean bean = new UserServiceBean();
bean.setEmail("bean@testing");
bean.setUsername("Frank");
UserServiceBean login = service.login(bean);
assertEquals(login.getEmail(), actual:"bean@testing");
IntelliJ 有一個簡潔的自動修復按鈕,可幫助您正確格式化代碼。 我的代碼現在可以正確運行到選定的 Junit 並且格式正確。
在您選擇修復代碼格式之前,請務必突出顯示 Assert.assertEquals(),以便 IntelliJ 會告訴您如何執行此操作-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.