2017-07-24 36 views
1

我想模擬枚舉的所有實例的方法shouldShow()。有人可以幫助如何做到這一點。模擬枚舉java的所有實例的方法

public enum MyButton implements ListButton { 
    Button1(
     R.drawable.drawable1) { 
      // Some methods here 
    }, 
    Button2(
      R.drawable.drawable2) { 
     //Some methods here 
    }; 

    @Override 
    public boolean shouldShow() { 
     //Some logic that decides whether to show the button or not 
    } 
} 

我想嘲弄它這樣如果我叫Button1.shouldShow或Button2.shouldShow,或enum.shouldShow任何其他值,我的存根應返回true。

+0

你想通過這樣做完成什麼?我的理解是,你不能以這種方式嘲笑枚舉,但通常你仍然可以通過其他方式實現你想要的測試。 –

+0

shouldShow方法包含一組靜態。我知道我可以使用powermock來測試函數,但是我將不得不模擬很多靜態方法。我試圖避免這樣做,以某種方式嘲弄shouldShow函數本身。 – Prateek

+0

抱歉誤會;這不是我所問的。在你的測試案例中,你試圖嘲笑這種方法,你在測試什麼?那個測試用例的更廣泛的目標是什麼? –

回答

0

我想shouldShow()方法是ListButton接口的一部分。如果你的設計經過深思熟慮,你很可能會嘲笑ListButton,而不是發生任何特定的MyButton

Java枚舉常量本質上是公共靜態final字段,因此嘲諷常量上的shouldShow()方法就等於嘲弄靜態final字段上的方法。