我有下面的代码片段,我不确定如何用单元测试覆盖默认子句。
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
类型。另外,我不想为了测试目的在我的枚举中添加任何虚拟值。