jmockit/jmockit1

Mock a superclass which is inherited from another class is not available

HexosR opened this issue · 1 comments

Version
JMockit: 1.49
Java Version: 1.8.0_202
Junit Version: 4.11

Description
Mocking a superclass which is inherited from another class is not working.
This problem will not occur if we will mock a method just in a leaf class, so the problem clearly is related to the inheritance.
I am guessing that this change is because of commit named "Changed the effect of a mocked base class on unmocked subclass instances, so they remain unmocked".
Can you elaborate on this problem because I can not see any explanation in release notes.
Is it a bug or just this kind of mocking is no longer supported?

Code to Reproduce

public class BugTest
{
	@Mocked BugTest.Base mockBase;

	@Test public void instanceOfMockedBaseClassIsMocked()
	{
		new Expectations() {{
			mockBase.foo();
			result = 42;
		}};
		Implementation impl = new Implementation();
		assertEquals(42, impl.foo());
	}

	static class Implementation extends Base
	{

	}

	static class Base
	{
		int foo()
		{
			return 10;
		}
	}
}

This sort of mocking proved to cause problems in the past, so it's no longer done. Maybe using @Capturing Base would be appropriate in this case, though.