2. In this example, we will use annotations so we have three ways to enable them.
UserServiceImplTest.java
123456789101112
@RunWith(MockitoJUnitRunner.class)//First method to enable mockito's annotationspublicclassUserServiceImplTest{@RulepublicMockitoRulerule=MockitoJUnit.rule();//Second method to enable mockito's annotations@Beforepublicvoidsetup(){MockitoAnnotations.initMocks(this);//Third method to enable mockito's annotations}}
Note: You can use one of them. I suggest the use of MockitoRule in favor of use another runners like org.junit.runners.Parameterized
3. UserServiceImpl HAS-A UserRepository. For that reason, create a UserRepository mock and it will be injected in UserServiceImpl. Then, we can mock UserRepository methods as you can see in step 3.