如何使用Mockito爲測試目的模擬枚舉?鑑於這種樣品枚舉:在Java中使用Mockito模擬枚舉
public enum TestEnum {
YES,
NO
}
,這一次使用枚舉的方法:
public static boolean WorkTheEnum(TestEnum theEnum) {
switch (theEnum) {
case YES:
return true;
case NO:
return false;
default:
// throws an exception here
}
}
我怎麼能嘲笑枚舉達到開關環路的默認分支? This answer表示Mockito不能嘲笑枚舉,但答案也在一年多以前提供。我可以在同一時間嘲笑枚舉,還是讓我讓分支保持未經測試?其他Mocking框架不能使用。
有這樣的hacky方法:[黑客入侵和修改「最終靜態」字段](http://www.javaspecialists.eu/archive/Issue161.html) – Jesper
枚舉就像靜態類,因此你可以'用mookito把它們剔除。在你的示例中,默認是無法訪問的,所以你不能測試它。 –
爲什麼要測試不可達路徑? – Manu