Jay*_*han 5 java android unit-testing mockito
我们如何模拟应用程序上下文?我有一个演示者,我打算为此编写测试。它接收的参数是view和Context。我如何创建一个模拟来使上下文起作用?
public TutorProfilePresenter(TutorProfileScreenView view, Context context){
this.view = view;
this.context = context
}
public void setPrice(float price,int selectedTopics){
int topicsPrice = 0;
if(selectedTopics>2)
{
topicsPrice = (int) ((price/5.0)*(selectedTopics-2));
}
view.setBasePrice(price,topicsPrice,selectedTopics,
price+topicsPrice);
}
Run Code Online (Sandbox Code Playgroud)
作为基础,我将使用Mockito批注(我假设您也想模拟视图):
public class TutorProfilePresenter{
@InjectMocks
private TutorProfilePresenter presenter;
@Mock
private TutorProfileScreenView viewMock;
@Mock
private Context contextMock;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test
public void test() throws Exception{
// configure mocks
when(contextMock.someMethod()).thenReturn(someValue);
// call method on presenter
// verify
verify(viewMock).setBasePrice(someNumber...)
}
}
Run Code Online (Sandbox Code Playgroud)
这样就可以为在测试中的类中配置模拟了。
有关Mockito存根的更多见解:sourceartists.com/mockito-stubbing
| 归档时间: |
|
| 查看次数: |
6630 次 |
| 最近记录: |