Hi,
That's why inheritance can be dangerous in object oriented programming, you really should choose composition over inheritance.
Inheritance can break encapsulation.
In this particular case I would agree with Felix. You might want to create an or several interfaces, then compose your object B with A or others.
Another solution, which is in my humble opinion a very probable code/design smell, is to extract your super.methodA() in a package protected method so you can stubIt.
eg:
class A {
void methodA() { }
}
class B extends A {
void methodA() {
if (some condition) { callSuperMethodA(); }
else { /* do something yet */ }
}
void callSuperMethodA() { super.methodA(); }
}
@RunWith(MockitoJUnitRunner.class)
public class BTestCase {
@Spy private B b;
@Test public void methodA() throws Exception {
doNothing().when(b.callSuperMethodA());
b.methodA();
// don't fail
}
}
Again I really would go for composition and delegating, that's more object oriented.
Hope that helps :)
--
Bryce