Die unten angegebene Methode im Codeabschnitt gibt void zurück. Ich möchte wissen, wie man eine Methode testet, die void zurückgibt. Ich überprüfte einige Antworten, aber die Verwendung "doThrow()" mit einer Ausnahme, aber in meinem Fall wirft die Methode keine AusnahmeSo testen Sie eine Void-Methode mit Mockito
Bitte lassen Sie mich wissen, wie kann ich eine Methode testen gibt void zurück?
Code
public void setImageOnImageView(RequestCreator requestCreator, ImageView imgView) {
requestCreator.into(imgView);
}
Test:
public class ValidationTest {
@Mock
private Context mCtx = null;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Before
public void setUp() throws Exception {
mCtx = Mockito.mock(Context.class);
Assert.assertNotNull("Context is not null", mCtx);
}
@Test
public void setImageOnImageView() throws Exception {
Uri mockUri = mock(Uri.class);
RequestCreator requestCreator = Picasso.with(mCtx).load(mockUri);
RequestCreator spyRequestCreator = spy(requestCreator);
ImageView imageView = new ImageView(mCtx);
ImageView spyImageView = spy(imageView);
doThrow().when(spyRequestCreator).into(spyImageView);
//spyRequestCreator.into(spyImageView);
}
}
danke, jetzt funktioniert es. Bitte sehen Sie sich meine Frage hier an: https: //stackoverflow.com/questions/46267298/method-not-mocked-using-spy Ich versuche eine Methode zu testen, aber der Testfall schlägt fehl – user2121