简体   繁体   English

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

[英]Intellij Gradle project cannot resolve assertEquals with junit 4.11 as the testCompile dep

I'm trying to set up a simple gradle project in the latest version of Intellij IDEA (13.0.2).我正在尝试在最新版本的 Intellij IDEA (13.0.2) 中设置一个简单的 gradle 项目。

I have no dependencies other than JUnit 4, my build.gradle file looks like this:除了 JUnit 4,我没有其他依赖项,我的 build.gradle 文件如下所示:

apply plugin: 'java'

sourceCompatibility = 1.5
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

I'm trying to use assertEquals in my Main class test suite, but Intellij is giving me the "Cannot resolve method assertEquals(int, int)" for the following two instances of it's use:我正在尝试在我的主类测试套件中使用 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 {

    }
}

Also, Intellij indicates tells me that the import org.junit.Assert.* isn't used.此外,Intellij 表示未使用 import org.junit.Assert.*。

If anyone knows why I'm having this problem, I'd really appreciate the help.如果有人知道我为什么会遇到这个问题,我将非常感谢您的帮助。 Thanks.谢谢。

import org.junit.Assert.*;

应该

import static org.junit.Assert.*;

I faced the same issue and fixed it by changing assertEquals to我遇到了同样的问题并通过将 assertEquals 更改为

Assert.assertEquals

and

import org.junit.Assert;

Hope this will be helpful for someone down the line.希望这对下线的人有所帮助。

Using IntelliJ 2019.2.4 with a start.sping.io default setup...使用 IntelliJ 2019.2.4 和 start.sping.io 默认设置...

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

but instead of但不是

Assert.assertEquals(expected, actual);

use

assertEquals(expected, actual);

So I just did intelliJ and had the same issues, my auto fix press option + enter button所以我只是做了 intelliJ 并遇到了同样的问题,我的自动修复按选项 + 输入按钮

it imported this:它导入了这个:

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

then auto fixed this for my test然后为我的测试自动修复这个

@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 has a neat auto-fix button that will help you format your code correctly. IntelliJ 有一个简洁的自动修复按钮,可帮助您正确格式化代码。 My code now runs correctly to Junit selected and is formatted correctly.我的代码现在可以正确运行到选定的 Junit 并且格式正确。

Be sure to highlight over the Assert.assertEquals(), before you opt to fix the format of your code, so IntelliJ will tell how to do that for you-在您选择修复代码格式之前,请务必突出显示 Assert.assertEquals(),以便 IntelliJ 会告诉您如何执行此操作-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM