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

maf*_*ffo 1 java junit exception-handling

我对使用Junit处理Java异常有点新意,我会非常感激.

我想做什么:

  • 我围绕创建新的CustomObject,try因为用户可以传入一个Stringenum我们调用时不匹配的valueof().我希望能够在这里捕获一个异常,但是我被告知:"应该避免捕获异常只能重新抛出异常的catch语句." 必须有更好的方法来处理这个问题?

  • 如果新对象具有正确的,enum那么我调用isValidObject,返回一个boolean.如果Integer无效则我throw是例外.

  • 我的测试有一个@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;
            }

        }
Run Code Online (Sandbox Code Playgroud)

哦,如果有人可以推荐任何关于异常处理的好东西,那就太好了.

ass*_*ias 5

应该避免捕获异常只能重新抛出它的catch语句.".必须有更好的方法来处理它吗?

是的,只需删除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]));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

并且您似乎接受一个null String作为有效条目,这可能不是一个好主意(显然取决于您的用例).

我可能会这样写:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,CustomObject的构造函数可能有意义地检查它的参数是否正常,而不必调用单独的isValid方法.那么最后一个块就是:

    return new CustomObject(type, value);
Run Code Online (Sandbox Code Playgroud)

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