It does only happen with whenNew. Normal when works:
@PrepareForTest(Rendering.class)
public class GameTest extends EngineTest {
private Rendering rendering;
@Before
public void setUp() throws Exception {
rendering = PowerMockito.mock(Rendering.class);
PowerMockito.when(rendering.getDisplayHeight()).thenReturn(new
Random().nextInt());
}
@Test
public void test1() {
System.out.println(rendering.getDisplayHeight());
}
@Test
public void test2() {
System.out.println(rendering.getDisplayHeight());
}
}
Gives the output:
74680897
1613028554
I don't know exactly what you mean by "how do you do your
verification?". You should be able to see the verify calls in the
example I posted at beginning of the thread, for example:
InOrder inOrder = inOrder(gameRunner);
inOrder.verify(gameRunner).switchToGameStage(gameStage);
inOrder.verify(gameRunner).update();
inOrder.verify(gameRunner).release();
verify(keyboard).release();
verify(rendering).release();
verify(audio).release();
I've taken a quick look into the code and I think this method of
DefaultConstructorExpectationSetup is causing the problem as it checks
if the class to inject constructors for has been mocked before and if
so just uses the one from the repository:
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> OngoingStubbing<T>
createNewSubsituteMock(Class<T> type, Class<?>[] parameterTypes,
Object... arguments) throws Exception {
if (type == null) {
throw new IllegalArgumentException("type
cannot be null");
}
final Class<T> unmockedType = (Class<T>)
WhiteboxImpl.getUnmockedType(type);
if (parameterTypes == null) {
WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments);
} else {
WhiteboxImpl.getConstructor(unmockedType,
parameterTypes);
}
/*
* Check if this type has been mocked before
*/
NewInvocationControl<OngoingStubbing<T>>
newInvocationControl = (NewInvocationControl<OngoingStubbing<T>>)
MockRepository
.getNewInstanceControl(unmockedType);
if (newInvocationControl == null) {
InvocationSubstitute<T> mock =
MockCreator.mock(InvocationSubstitute.class, false, false, null, null,
(Method[]) null);
newInvocationControl = new
MockitoNewInvocationControl(mock);
MockRepository.putNewInstanceControl(type,
newInvocationControl);
MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getUnmockedType(type));
}
return
newInvocationControl.expectSubstitutionLogic(arguments);
> Thanks for your investigation, very good! Does this only happen when you're
> using "whenNew" or does it also happens on standard "when"? And also, how do
> you do your verification?
>
> /Johan
>
> On Mon, Nov 15, 2010 at 11:48 AM, Alex
> <
alexander.weickm...@googlemail.com>wrote:
> > <
powermock%2Bunsu...@googlegroups.com<
powermock%252Buns...@googlegroups.com>