gpt4 book ai didi

Android JUnit 参数是字符串 "null"而不是 `null`

转载 作者:行者123 更新时间:2023-12-05 07:47:01 24 4
gpt4 key购买 nike

我正在尝试运行一些参数集中为 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
}

...
}

最佳答案

我最终在构造函数中初始化参数,并使用条件检查将字段设置为 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...*/

}

关于Android JUnit 参数是字符串 "null"而不是 `null`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40250799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com