I have a method in my MainActivity (Android) and I want to mock the A instance :
public void some_method() { A a = new A(); .... } so I created a kind of factory class as such
public class SomeFactory(){ // some constructor public A populateWithParameter(Parameter parameter){ return new A(parameter) } } and the method above turns into
public void some_method(SomeFactory someFactory) { A a = someFactory.populateWithParameter(parameter); a.method_call() .... } I tried this
@Mock SomeFactory someFactory; public void testSomeMethod() throws Exception { SomeFactory someFactory = new SomeFactory(); when(someFactory.populateWithParameter( some_parameter)).thenReturn(null); mainActivity.some_method(someFactory); ... } but I get this error message
org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);