how to test picasso using unit-test and mockito

有些话、适合烂在心里 提交于 2019-12-25 08:49:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!