我们在 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/