sto cercando di verificare che il metodo Collections.shuffle() è stato chiamato in una delle mie classi. Ho letto la (piccola) documentazione su PowerMock con Mockito e ho letto le altre domande su SO che trattavano questo problema e non ottenevo una soluzione.beffardo statico con PowerMock e Mockito non funziona
@RunWith(PowerMockRunner.class)
@PrepareForTest(Collections.class)
public class MyTest {
@Test
public void testShuffle() {
PowerMockito.mockStatic(Collections.class);
PowerMockito.doCallRealMethod().when(Collections.class);
Collections.shuffle(Mockito.anyListOf(Something.class));
ClassToTest test = new ClassToTest();
test.doSomething();
PowerMockito.verifyStatic(); // Verify shuffle was called exactly once
Collections.shuffle(Mockito.anyListOf(Something.class));
}
}
public class ClassToTest {
private final List<Something> list;
// ...
public void doSomething() {
Collections.shuffle(list);
// do more stuff
}
}
Dato il codice di cui sopra, mi aspetto il passaggio di test unitario. Tuttavia, il test dell'unità non riesce come segue:
Wanted but not invoked java.util.Collections.shuffle([]);
Actually, there were zero interactions with this mock.
Cosa sto facendo male?
Grazie
EDIT: Come per il suggerimento di seguito ho provato quanto segue e ottengo lo stesso errore.
@RunWith(PowerMockRunner.class)
@PrepareForTest(Collections.class)
public class MyTest {
@Test
public void testShuffle() {
PowerMockito.mockStatic(Collections.class);
ClassToTest test = new ClassToTest();
test.doSomething();
PowerMockito.verifyStatic(); // Verify shuffle was called exactly once
Collections.shuffle(Mockito.anyListOf(Something.class));
}
}
Cosa succede se si allentare le restrizioni sulla matcher da '' anyListOf' a qualsiasi (List.class) '? Cosa succede se aggiungete 'ClassToTest' all'annotazione' @ PrepareForTest'? –