【发布时间】:2016-07-15 16:52:42
【问题描述】:
我的 MainActivity (Android) 中有一个方法,我想模拟 A 实例:
public void some_method() {
A a = new A();
....
}
所以我创建了一种工厂类
public class SomeFactory(){
// some constructor
public A populateWithParameter(Parameter parameter){
return new A(parameter)
}
}
上面的方法变成了
public void some_method(SomeFactory someFactory) {
A a = someFactory.populateWithParameter(parameter);
a.method_call()
....
}
我试过了
@Mock
SomeFactory someFactory;
public void testSomeMethod() throws Exception {
SomeFactory someFactory = new SomeFactory();
when(someFactory.populateWithParameter(
some_parameter)).thenReturn(null);
mainActivity.some_method(someFactory);
...
}
但我收到此错误消息
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);
【问题讨论】:
标签: java android unit-testing mocking