When spying on Powermocked classes, you should use the PowerMockito.spy(...) method:
VTDGen vtgGenSpy = PowerMockito.spy(vtdGen);
Also make sure your combinations of Mockito / Powermock versions are compatible. I am using Mockito 1.8.5 and Powermock 1.4.10.
The following tests pass for me (TestNG):
@PrepareForTest({VTDGen.class, Uo.class}) public class UoTest extends PowerMockTestCase { private VTDGen vtdGen; private VTDGen vtdGenSpy; private Uo unitUnderTest; @BeforeMethod public void setup() { vtdGen = new VTDGen(); vtdGenSpy = PowerMockito.spy(vtdGen); unitUnderTest = new Uo(); } @Test public void testWithSpy() throws Exception { PowerMockito.whenNew(VTDGen.class).withNoArguments().thenReturn(vtdGenSpy); boolean result = unitUnderTest.parse(); assertTrue(result); } @Test public void testWithoutSpy() throws Exception { PowerMockito.whenNew(VTDGen.class).withNoArguments().thenReturn(vtdGen); boolean result = unitUnderTest.parse(); assertTrue(result); } }
For the device under test:
public class Uo { public boolean parse() { VTDGen vg = new VTDGen(); return vg.parseFile("test.xml", false); } }
orien source share