2016-04-06 40 views
1

时更换StricAssertions断言我们有一个进口的问题与Eclipse:Eclipse的组织导入

测试类使用Assertions.assertThat

当按下Ctrl + Shift + O组织进口,Eclipse的替代断言。 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(); 
    } 
} 

看来,断言延伸StrictAssertions,所以他们是使用StrictAssertions没有问题的,但为什么就是Eclipse不使用扩展的类?

回答

1

看起来像,因为assertThat(int actual)定义在StrictAssertions而不是隐藏的Assertions,Eclipse决定从StrictAssertions导入。

此外,为组织进口Eclipse似乎忽略类型过滤器 - 所以即使这不会帮助。

看来,断言延伸StrictAssertions,所以他们是使用StrictAssertions

不适合您当前设置没有问题,但StrictAssertions已与AssertJ 3.2.0删除。所以当升级到更新版本的AssertJStrictAssertions将会阻碍你。

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

+0

好吧,看来测试使用 assertThat(文件).hasSameContentAs(refFile); 没有通过任何更多... 但试图检查2个虚拟文件时,它正在工作... – GaspardP

+0

@GaspardP嗯,在我的测试工作。你设置/检查文件编码? – nyname00

+0

那么,我们找到了一个方法: hasSameContent比较V3.2.0中的2个字符串,并与V3.1.0中的二进制文件进行比较。 ,在我们的测试中,我们需要比较二进制文件。 – GaspardP