Intellij Gradle项目无法使用junit 4.11作为testCompile dep解析assertEquals

时间:2014-02-06 18:45:23

标签: java junit intellij-idea gradle

我正在尝试在最新版本的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'
}

我正在尝试在我的Main类测试套件中使用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指示告诉我没有使用导入org.junit.Assert。*。

如果有人知道我为什么遇到这个问题,我真的很感激帮助。 感谢。

4 个答案:

答案 0 :(得分:20)

import org.junit.Assert.*;

应该是

import static org.junit.Assert.*;

答案 1 :(得分:1)

我遇到了同样的问题,并通过将assertEquals更改为

来解决它
Assert.assertEquals

import org.junit.Assert;

希望这对下线的人会有所帮助。

答案 2 :(得分:0)

使用IntelliJ 2019.2.4和start.sping.io默认设置...

import static org.junit.jupiter.api.Assertions.assertEquals;

但不是

Assert.assertEquals(expected, actual);

使用

assertEquals(expected, actual);

答案 3 :(得分:0)

所以我只是做了 intelliJ 并且遇到了同样的问题,我的自动修复 press option + enter button

它导入了这个:

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 会告诉您如何执行此操作-