我们如何模拟应用程序上下文?我有一个主持人打算为了编写测试。它收到的参数为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);
}
作为基础,我将使用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...)
}
}
这将注入准备将模拟配置为正在测试的课程中。
更多关于摩擦脚的洞察力:sourceartists.com/mockito-stubbing