gpt4 book ai didi

Java 使用 junit 检查

转载 作者:行者123 更新时间:2023-12-01 09:30:44 25 4
gpt4 key购买 nike

我有这段代码,我尝试对其运行 Junit 测试,但收到一个错误,指出 nullpointerexception 我对 Java 相当陌生,因此感谢任何和所有帮助。谢谢。

/**
* If the ip address from the String passed is valid,
* sets the instance variable parts to store it as 4 integer values.
* For example, if ip = "192.000168.0.0000001", parts should become {192,168,0,1}.
* If the ip address passed is invalid, parts should become {0,0,0,0}
*
* remember to reset the instance array parts before you do anything else
* @param ip
*/
public void setParts(String ip) {
boolean checkiftrue = isValidElement(ip);
String[] IPString = ip.split("\\.");

if (checkiftrue = true) {
for (int i = 0; i < IPString.length; i++)
parts[i] = Integer.valueOf(IPString[i]);
}
else {
parts = new int[]{0,0,0,0};
}
}

我用来测试的 Junit 是

public void testSetPartsString() {
correct1.setParts("12.14.16.18");
int[] a = correct1.getParts();
assertEquals(4, a.length);
assertEquals(12, a[0]);
assertEquals(14, a[1]);
assertEquals(16, a[2]);
assertEquals(18, a[3]);

correct1.setParts("-12.14.16.18");
a = correct1.getParts();
assertEquals(4, a.length);
assertEquals(0, a[0]);
assertEquals(0, a[1]);
assertEquals(0, a[2]);
assertEquals(0, a[3]);

correct1.setParts("255.255.255.255");
a = correct1.getParts();
assertEquals(4, a.length);
assertEquals(255, a[0]);
assertEquals(255, a[1]);
assertEquals(255, a[2]);
assertEquals(255, a[3]);

correct1.setParts("12.314.16.18");
a = correct1.getParts();
assertEquals(4, a.length);
assertEquals(0, a[0]);
assertEquals(0, a[1]);
assertEquals(0, a[2]);
assertEquals(0, a[3]);

correct1.setParts("255.255.255.255");

correct1.setParts("12.16.18");
a = correct1.getParts();
assertEquals(4, a.length);
assertEquals(0, a[0]);
assertEquals(0, a[1]);
assertEquals(0, a[2]);
assertEquals(0, a[3]);

correct1.setParts("255.255.255.255");

correct1.setParts("12.16.18.20.22");
a = correct1.getParts();
assertEquals(4, a.length);
assertEquals(0, a[0]);
assertEquals(0, a[1]);
assertEquals(0, a[2]);
assertEquals(0, a[3]);
}

Error is here

    public static boolean isValidElement(String token) {
try {
String[] validString = token.split("\\.");
if (validString.length != 4) return false;
for (String checkvalidstring: validString ) {
int validstringchecker = Integer.parseInt(checkvalidstring);
if ((validstringchecker < 0) || (validstringchecker > 255)) return false;
System.out.println(validstringchecker);
}
} catch (NumberFormatException errorcheck){
return false;
}
return true;
}

最佳答案

一个典型的错误。你有

    if (checkiftrue = true) {

这会将 true 分配给 checkiftrue 并始终评估为 true。因此,即使 IP 无效,您也会转到第 56 行,并且会发生异常。相反,只需:

    if (checkiftrue) {

(或使用 ==,但实际上,我发现不使用它更具可读性)。

PS 我发现是因为我的 Eclipse 在 boolean checkiftrue = isValidElement(ip); 行上说“未使用局部变量 checkiftrue 的值”。如果您花时间了解这些警告的含义,这些警告通常会非常有帮助。

关于Java 使用 junit 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39433576/

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