Introduction 為了減少大家重複撰寫mock、spy或captor等語法,Mockito提供了 annotation 去解決這個問題。此篇教學已經寫的非常淺顯易懂,我只針對它不足的部分做補充。 Enable Mockito Annotations 要使用這個功能之前,必須要去啟用它,最常使用的方式就是直接使用MockitoJUnitRunner當Runner: @RunWith ( MockitoJUnitRunner. class ) public class CheckConnectionCommandTest { // skip } 假如你已經使用了PowerMockRunner或SpringJUnit4ClassRunner當Runner,你就要從以下兩個方法選一個出來用。一個是在testcase setup去初始化: @Before public void init ( ) { MockitoAnnotations. initMocks ( this ) ; } 另外一個就是透過@Rule的方式: @Rule public MockitoRule initRule = MockitoJUnit. rule ( ) ; Reference Getting Started with Mockito @Mock, @Spy, @Captor and @InjectMocks