0

I am using the below code to assert text in my test script. But its giving UnsupportedOperationException error every time it hits this code.

public static void verifyEquals(Object actual, Object expected) {
        try {
            Assert.assertEquals(actual, expected);
        } catch(Throwable e) {
            addVerificationFailure(e);
        }
    }

    public static List<Throwable> getVerificationFailures() {
        List verificationFailures = verificationFailuresMap.get(Reporter.getCurrentTestResult());
        return verificationFailures == null ? new ArrayList() : verificationFailures;
    }

    private static void addVerificationFailure(Throwable e) {
        StackTraceElement[] error = e.getStackTrace();
        List<StackTraceElement> errors = Arrays.asList(error);
        verificationFailuresMap.put(Reporter.getCurrentTestResult(), errors);
        List verificationFailures = getVerificationFailures();
        verificationFailuresMap.put(Reporter.getCurrentTestResult(), verificationFailures);
        verificationFailures.add(e);
    }

Can anyone help me on this?

4

1 回答 1

2

这就是问题:

List verificationFailures = getVerificationFailures();
verificationFailuresMap.put(Reporter.getCurrentTestResult(), verificationFailures);
verificationFailures.add(e);

您正在调用List.add. Arrays.asList你不能这样做,因为结果Arrays.asList是数组的视图- 你不能添加或删除元素。

此外 - 除了擦除 - 该列表是 a List<StackTraceElement>- 它由StackTraceElement[]. 添加一个甚至意味着什么Throwable

如果您删除将列表放入地图的代码,然后在添加之前再次将其取回,则更容易看到问题:

private static void addVerificationFailure(Throwable e) {
    StackTraceElement[] error = e.getStackTrace();
    List<StackTraceElement> errors = Arrays.asList(error);
    verificationFailuresMap.put(Reporter.getCurrentTestResult(), errors);
    errors.add(e);
}

由于尝试将 a 添加到 a ,这现在将在编译时失败。但是即使您尝试添加 a它仍然会由于不支持而失败。ThrowableList<StackTraceElement>StackTraceElementArrays.asListadd

真的不清楚你要做什么,但你需要重新考虑......

于 2013-09-18T06:24:09.300 回答