Android JUnit参数是String" null"而不是'null`

时间:2016-10-25 22:30:59

标签: android android-testing android-junit

我试图在参数集中使用null运行一些新的参数化Android测试。不幸的是,这些值显示为字符串" null" 而不是null。我该如何防止这种情况发生?

@RunWith(Parameterized.class)
public class PedigreeProviderTest {

    @Parameter()
    @SuppressWarnings("WeakerAccess")
    public String mVin;
    @Parameter(value = 1)
    @SuppressWarnings("WeakerAccess")
    public String mDap;
    @Parameter(value = 2)
    @SuppressWarnings("WeakerAccess")
    public int mAddress;
    @Parameter(value = 3)
    @SuppressWarnings("WeakerAccess")
    public int mBusId;

    @Parameters
    public static Collection<Object[]> parameters() {
        return Arrays.asList(new Object[][]{
                {null, null, 0x7F, 0},
                {"1FUYFXYB3XPA96364", null, 0x7F, 0},
                {"1HD4CAM30YK190948", "00D06948C67C", 0x7F, 0},
                {null, "00D06948C67C", 0x7F, 0},
        });
    }

    @Test
    public void testSomething(){
        "null".equals(mVin); //is true for parameter 0 and 3
        mVin == null; //is never the case
    }

    ...
}

1 个答案:

答案 0 :(得分:0)

我最终在构造函数中初始化参数,并使用条件检查将字段设置为null&#34; null&#34;

@RunWith(Parameterized.class)
public class PedigreeProviderTest {
    private String mVin;
    private String mDap;
    private int mAddress;
    private int mBusId;

    @Parameters
    public static Iterable<Object[]> parameters() {
        return Arrays.asList(new Object[][]{
                {"1FUYFXYB3XPA96364", null, 0x7F, 0},
                {"1HD4CAM30YK190948", "00D06948C67C", 0x7F, 0},
                {null, "00D06948C67C", 0x7F, 0},
                {null, null, 0x7F, 0},
        });
    }

    public PedigreeProviderTest(String vin, String dap, int address, int busId) {
        mVin = "null".equals(vin) ? null : vin;
        mDap = "null".equals(dap) ? null : dap;
        mAddress = address;
        mBusId = busId;
    }

    /*tests...*/

}