Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can either mock/verify the java.util.Collections.shuffle([]) method or call the real implementation (with PowerMockito.doCallRealMethod()). But you can't do both.</p> <p>If you remove </p> <pre><code>PowerMockito.doCallRealMethod().when(Collections.class); </code></pre> <p>the call will be verified and the test will pass.</p> <p><a href="https://powermock.googlecode.com/svn/docs/powermock-1.4.7/apidocs/org/powermock/api/mockito/PowerMockito.html#doCallRealMethod%28%29" rel="nofollow">https://powermock.googlecode.com/svn/docs/powermock-1.4.7/apidocs/org/powermock/api/mockito/PowerMockito.html#doCallRealMethod%28%29</a></p> <p>This code works for me:</p> <pre><code>package test; import java.util.Collections; import java.util.LinkedList; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(Collections.class) public class MyTest { @Test public void testShuffle() { PowerMockito.mockStatic(Collections.class); /* PowerMockito.doCallRealMethod().when(Collections.class); remove this line */ 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)); } } class ClassToTest { private List&lt;Something&gt; list = new LinkedList&lt;Something&gt;(); // ... public void doSomething() { Collections.shuffle(list); // do more stuff } } class Something { } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload