Covering unreachable default clause in switch case in java

问题描述 投票:0回答:0

我有下面的代码片段,我不确定如何用单元测试覆盖默认子句。

public Block solve(String str) {

    try {
            
        final BlockType blockType = BlockType.valueOf(str);

        switch (blockType) {
            case TEXTBLOCK:
                return ...
            case IMAGEBLOCK:
                return ...
            default:
                return null;
        }
    } catch (final IllegalArgumentException e) {
        return null;
    }
}
@AllArgsConstructor
public enum BlockType {
    IMAGEBLOCK("imageBlock"),
    TEXTBLOCK("textBlock");

    private String value;
}

注意:我有一个约束,即

solve
方法中的参数只能是
String
类型。另外,我不想为了测试目的在我的枚举中添加任何虚拟值。

java junit enums mockito switch-statement
© www.soinside.com 2019 - 2024. All rights reserved.