Eclipse存在导入问题:
测试类使用Assertions.assertThat
当按Ctrl + Shift + O组织导入时,Eclipse会将Assertions.assert替换为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不使用扩展类?
答案 0 :(得分:1)
看起来,因为assertThat(int actual)
在StrictAssertions
中定义且未被Assertions
隐藏,所以Eclipse决定从StrictAssertions
导入。
此外,对于组织导入,Eclipse似乎忽略了类型过滤器 - 所以即使这样也不会有帮助。
似乎Assertions扩展了StrictAssertions,因此使用StrictAssertions时它们没有问题
不适用于您当前的设置,但StrictAssertions
已被AssertJ 3.2.0删除。因此,当升级到较新版本的AssertJ
StrictAssertions
时,您会遇到困难。
我建议你升级到 3.2.0或更高版本,如果你的项目可以的话。