@InjectMocks Annotation




Example:
class MyData{
private ArrayList<String> list;
MyData(){
list = new ArrayList<String>();
list.add("Good Morning");
list.add("Good Noon");
list.add("Good Afternoon");
list.add("Good Evening");
list.add("Good Night");
}

public String getValue(int index){
return list.get(index);
}
}
public class InjectMockTestFlight {

@Mock 
ArrayList<String> list;

@InjectMocks
MyData myData;

@Before
public void init(){
MockitoAnnotations.initMocks(this);
}

@Test
public void test_InjectMocks(){
Mockito.when(myData.getValue(0)).thenReturn("Good Morning");
}
}
It will inject the mock object into the @InjectMocks annotated object.
In below example myData required the list object.
myData annotated with the @InjectMocks and it will take the List @Mock annotated object.