I have a scenario where I have an abstract class similar to the below. I want to mock out the RandomNumberGenerator
instance that is supplied via the constructor and I want to write a unit test to test the logic in the doSomethingRandomLotsOfTimes
method.
Is there a way I can do this with mockito/another framework?
public abstract class MyClass {
private final RandomNumberGenerator randomNumberGenerator;
public MyClass(RandomNumberGenerator randomNumberGenerator) {
this.randomNumberGenerator = randomNumberGenerator;
}
public abstract void doSomething(int x);
public void doSomethingRandomLotsOfTimes() {
for(int i=0; i<10; i++) {
int r = randomNumberGenerator.next();
doSomething(r);
}
}
}
My Test
@Test
public void testItDoesSomething10Times() {
// Given
when(randomNumberGenerator.next()).thenReturn(0);
MyClass myClass = ...
// When
myClass.doSomethingRandomLotsOfTimes();
// Then
// Assert Something
}