Mocking SecureRandom::nextInt()
问题 I have a class that uses a SecureRandom instance and grabs the next random number. Lets say the example is: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } Test code @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockito.spy(new