我正在尝试运行一些新的参数化Android测试,在参数集中为null。不幸的是,这些值显示为字符串"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
}
...
}
我最终在构造函数中初始化参数并使用条件检查将字段设置为null
,当它们"null"
@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...*/
}