![](/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.