问题
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?
code
public RequestCreator requestCreatorFromUrl(String mPicUrl)
{
return Picasso.with(mCtx).load(mPicUrl);
}
public void setImageOnImageView(RequestCreator requestCreator, ImageView mImagView)
{
requestCreator.into(mImagView);
}
My Attempts:
@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));
}
回答1:
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);
}
and
public void load(RequestCreator requestCreator, String picUrl) {
requestCreator.load(picUrl)
}
and test the load(...)
method.
Second method:
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);
来源:https://stackoverflow.com/questions/46262467/how-to-test-picasso-using-unit-test-and-mockito