java - Eclipse 在组织导入时用 StricAssertions 替换 Assertions

标签 java eclipse import assertions

我们在 Eclipse 中遇到导入问题:

测试类使用 Assertions.assertThat

当按 Ctrl + Shift + O 组织导入时,Eclipse 将 Assertions.assertThat 替换为 StrictAssertions.assertThat

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class TheTest {

    @Test
    public void testName() {
        assertThat(2).isEqualTo(2);
    }
}

替换为:

import static org.assertj.core.api.StrictAssertions.assertThat;  // change here !

import org.junit.Test;

public class TheTest {

    @Test
    public void testName() {
        assertThat(2).isEqualTo(2);
    }
}

当我们有一些仅在断言(对于列表)中的特定断言时,Eclipse 将 StrictAssertions 添加到导入中。

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;

import org.junit.Test;

public class TheTest {

    @Test
    public void testName() {
        assertThat(2).isEqualTo(2);
        assertThat(new ArrayList<>()).isEmpty();
    }
}

更改为:

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.StrictAssertions.assertThat;  // this import was added 

import java.util.ArrayList;

import org.junit.Test;

public class TheTest {

    @Test
    public void testName() {
        assertThat(2).isEqualTo(2);
        assertThat(new ArrayList<>()).isEmpty();
    }
}

看来 Assertions 扩展了 StrictAssertions,所以使用 StrictAssertions 没有问题,但为什么 Eclipse 不使用扩展类?

最佳答案

看起来,因为 assertThat(int effective) 是在 StrictAssertions 中定义的,并且没有被 Assertions 隐藏,所以 Eclipse 决定从 导入>严格断言

此外,为了组织导入,Eclipse 似乎忽略了类型过滤器 - 所以即使这样也无济于事。

It seems that Assertions extends StrictAssertions, so their is no problem using StrictAssertions

不适合您当前的设置,但 StrictAssertions 已通过 AssertJ 3.2.0 删除。因此,当升级到较新版本的 AssertJ 时,StrictAssertions 将会妨碍您。

如果您的项目可行,我建议您升级到3.2.0 或更高版本

关于java - Eclipse 在组织导入时用 StricAssertions 替换 Assertions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36445207/

相关文章:

java - 在哪里可以设置spark.eventLog.enabled?

c# - 在 C#/.Net 中创建/切换桌面

CSS @import 不适用于本地文件 - 文件路径正确

java - 如何防止异常时自动递增?

java - JVM 执行代码以构建对象的顺序

java - R无法解决(XML问题但找不到)

java - 使用 JDBC 将 Microsoft Access 数据库连接到 Java - 错误

java - Eclipse 不会在设备上运行 Android 应用程序

Java 13 : Why are javaFX runtime components missing?

python - 无法导入模块 'lambda_function' : No module named '_speech_py_impl'