gpt4 book ai didi

java - 如何使用 Junit 测试处理 try/catch 和 throw

转载 作者:行者123 更新时间:2023-11-29 08:02:47 25 4
gpt4 key购买 nike

我对使用 Junit 在 Java 中处理异常有点陌生,非常感谢您提供一点指导。

我正在尝试做的事情:

  • 我用 try 围绕新 CustomObject 的创建因为用户可以传入 Stringenum 不匹配当我们调用valueof() .我希望能够在这里捕捉到一个异常,我就是这样,尽管我被告知:“应该避免捕捉到异常只是为了重新抛出它的 catch 语句。”。一定有更好的方法来处理这个问题吗?

  • 如果新对象有正确的enum然后我调用isValidObject ,它返回一个 boolean .如果Integer无效那么我throw一个异常(exception)。

  • 我的测试有一个 @Test(expected = AssertionError.class)并且正在过去。

是否有更好/更简洁的方法来使用异常?

我有以下代码:

private CustomObject getObjectFromString(String objectDataString) {
if (objectDataString != null) {
String[] customObjectComponents = objectDataString.split(":");
try {
CustomObject singleObject = new CustomObject(EnumObjectType.valueOf(customObjectComponents [0]),
Integer.parseInt(customObjectComponents [1]));
if (isValidCustomObject(singleObject)) {
return singleObject;
} else {
throw new IllegalArgumentException("Unknown custom object type/value: " + EnumObjectType.valueOf(customObjectComponents [0]) + ":"
+ Integer.parseInt(customObjectComponents [1]));
}
} catch (IllegalArgumentException e) {
throw e;
}

}

哦,如果有人能推荐任何关于异常处理的好书,那就太好了。

最佳答案

A catch statement that catches an exception only to rethrow it should be avoided.". There must be a better way to handle this?

是的,只需删除 try catch。您的代码相当于:

private CustomObject getObjectFromString(String objectDataString) {
if (objectDataString != null) {
String[] customObjectComponents = objectDataString.split(":");
CustomObject singleObject = new CustomObject(EnumObjectType.valueOf(customObjectComponents[0]),
Integer.parseInt(customObjectComponents[1]));
if (isValidCustomObject(singleObject)) {
return singleObject;
} else {
throw new IllegalArgumentException("Unknown custom object type/value: " + EnumObjectType.valueOf(customObjectComponents[0]) + ":"
+ Integer.parseInt(customObjectComponents[1]));
}
}
}

该代码将抛出 IllegalArgumentException如果值传递给您的 enum.valueOf()无效或者如果您的 isValidCustomObject方法返回 false。

请注意,它也可能会抛出 IndexOutOfBoundException。如果字符串不包含 :你可能想在调用 customObjectComponents[1] 之前测试它.它也可能抛出 NumberFormatException。

而且您似乎接受空字符串作为有效条目,which is probably not a good idea (显然取决于您的用例)。

我可能会这样写:

private CustomObject getObjectFromString(String objectDataString) {
Objects.requireNonNull(objectDataString, "objectDataString should not be null");
String[] customObjectComponents = objectDataString.split(":");
if (customObjectComponents.length != 2) {
throw new IllegalArgumentException("Malformed string: " + objectDataString);
}

EnumObjectType type = EnumObjectType.valueOf(customObjectComponents[0]);
try {
int value = Integer.parseInt(customObjectComponents[1]);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(customObjectComponents[1] + " is not an integer);
}

CustomObject singleObject = new CustomObject(type, value);
if (isValidCustomObject(singleObject)) {
return singleObject;
} else {
throw new IllegalArgumentException("Unknown custom object type/value: " + type + ":" + value);
}
}

最后,CustomObject 的构造函数自行检查其参数是否正确可能是有意义的,而不必调用单独的 isValid 方法。最后一个 block 将是:

    return new CustomObject(type, value);

如果需要,它会从构造函数中抛出 IllegalArgumentException。

关于java - 如何使用 Junit 测试处理 try/catch 和 throw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13388390/

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