javajunitmockito

@Mock object instance is null


@Mock object instance is null .

I am doing this is my test class

@Mock
private SchoolRequest schoolRequest;

@InjectMock
private FormRequest formRequest;

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

Class FormRequest {

@Autowired 
private SchoolRequest schoolRequest;


}

This application is running properly, but in test cases schoolRequest is coming as null. Any pointer on why it might happen ?


Solution

  • I don't know the reason but this worked.

    private SchoolRequest schoolRequest = Mockito.mock(SchoolRequest.class); 
    

    May be someone can comment why this worked. What is difference between @Mock and Mockito.mock . I thought they are same.