问题描述
我正在学习如何在android studio中进行单元测试.如下所示,我想测试下面在代码部分中显示的两种方法.
I am learning how to unit-testing in android studio. as shown below, I would like to test the two methods shown below in the code section.
能否请您帮助并指导我如何测试此方法?
can you please help and guide me how to test this method?
代码
public RequestCreator requestCreatorFromUrl(String mPicUrl)
{
return Picasso.with(mCtx).load(mPicUrl);
}
public void setImageOnImageView(RequestCreator requestCreator, ImageView mImagView)
{
requestCreator.into(mImagView);
}
我的尝试:
@Test
public void whenRequestCreatorFromUrlTest() throws Exception {
Picasso mockPicasso = mock(Picasso.class);
File mockFile = mock(File.class);
Assert.assertNotNull("returned Request creator is not null",
mockPicasso.load(mockFile));
}
推荐答案
第一个无法测试的方法,您必须验证对Mockito不支持的静态方法的调用.您可以在
First method you can't test, you'd have to verify the call of a static method which is not supported in Mockito.You could split the method in
public RequestCreator requestCreator() {
return Picasso.with(mCtx);
}
和
public void load(RequestCreator requestCreator, String picUrl) {
requestCreator.load(picUrl)
}
并测试load(...)
方法.
第二种方法:
模拟requestCreator
.模拟imageView
.用模拟对象调用该方法.然后验证是否使用提供的参数调用了requestCreator.into(...)
:
Mock the requestCreator
. Mock the imageView
.Call the method with your mocked objects.Then verify requestCreator.into(...)
was called with the supplied parameter:
Mockito.verify(requestCreator).into(imageView);
这篇关于如何使用单元测试和Mockito测试毕加索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!