小编co.*_*har的帖子

java.lang.ClassCastException:无法将Z强制转换为java.lang.String

我遇到一个错误:java.lang.ClassCastException: Z cannot be cast to java.lang.String尝试在Junit测试上运行coverage(EclEmma)时。如果我定期运行测试(没有覆盖范围),则测试通过。

这是代码(该类中的所有字段均为Strings):

@Override
public Map<String, String> getErrors() throws IllegalAccessException, IllegalArgumentException {

    Map<String, String> errors = new HashMap<String, String>();

    for (Field field : this.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String value = (String) field.get(this);

        if (value.equals("N")) {
            if (!errors.containsKey(field.getName())) {
                errors.put(field.getName(), value);
            }
        }
    }
    return errors;
}
Run Code Online (Sandbox Code Playgroud)

java junit classcastexception eclemma

3
推荐指数
1
解决办法
2482
查看次数

标签 统计

classcastexception ×1

eclemma ×1

java ×1

junit ×1