In the following example code one test fails because the real method is called, although the class that declares the method is mocked. It occurs if the tested class instance is partially mocked and a sub class of the mocked class.
public abstract class AbstractClass {
protected boolean isRendered() {
System.out.println("### real isRendered() is called");
return false;
}
}
public class ImplementationClass extends AbstractClass {}
public class ImplementationClassTest {
@Tested private ImplementationClass implementationClass;
@Mocked private AbstractClass mockAbstractClass;
@Test public void testWillSucceedBecauseIsRenderedIsMockedWithExpectedResultAtAbstractClass() throws Exception {
new NonStrictExpectations() {{ mockAbstractClass.isRendered(); result = true; }};
assertThat( implementationClass.isRendered(), is( true ));
}
@Test public void testWillSucceedBecauseIsRenderedIsMockedWithExpectedResultAtImplementationClass() throws Exception {
new NonStrictExpectations( implementationClass ) {{ implementationClass.isRendered(); result = true; }};
assertThat( implementationClass.isRendered(), is( true ));
}
@Test public void testWillUnexpectedlyFailBecauseTheRealIsRenderedIsCalled() throws Exception {
// just the declaration for the partial mock let the test fail,
// it is not necessary to record any method calls for the partial mock
new NonStrictExpectations( implementationClass ) {{ mockAbstractClass.isRendered(); result = true; }};
assertThat( implementationClass.isRendered(), is( true ));
}
}
In the following example code one test fails because the real method is called, although the class that declares the method is mocked. It occurs if the tested class instance is partially mocked and a sub class of the mocked class.