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

Eclipse replace Assertions with StricAssertions when organizing imports

我们遇到了 Eclipse 的导入问题:

测试class使用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 不使用扩展的 class?

看起来,因为 assertThat(int actual)StrictAssertions 中定义并且没有被 Assertions 隐藏,Eclipse 决定从 StrictAssertions 导入。

此外,为了组织导入,Eclipse 似乎忽略了 Type Filters - 所以即使那样也无济于事。

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

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

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