I am trying to test some code using a PowerMockito spy, and I am stubbing a method (getRootTagMap--see below) to return a value constructed in the tester, (Using PowerMockito, because the method is private .)
However instead of returning the value, it is always invoking the actual method, rather than returning the constructed value.
Not sure what I am doing wrong
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.mockito.PowerMockito.spy;
@RunWith(PowerMockRunner.class)
@PrepareForTest({JsonAppMessageProcessor.class})
public class TestPropStoreAppController {
@Test public void testSaveJsonAppTagChangesToPropStore() throws Exception {
JsonAppMessageProcessor messageProcessorSpy = spy(new JsonAppMessageProcessor());
when(messageProcessorSpy, "getRootTagMap", any(JsonAppTag.class)).thenReturn(constructReturnValue());
// I tried it this way too...
// doReturn(constructReturnValue()).when(messageProcessorSpy, "getRootTagMap", any(JsonAppTag.class));
// the following call calls the real getRootTagMap(JsonAppTag) method instead of returning the stub
messageProcessorSpy.saveChanges(constructParameterForChanges());
}
}