Search code examples
mockitoprivatepowermockpowermockito

How do I TEST (not mock) a private method using Mockito and/or PowerMock?


Every time I think I've found an example of this, it turns out to be that it's an example of mocking rather than testing. Note that the answer might be "don't do it that way" which I expect it might. I see lots of folks just using reflection but i was wondering if there's an easier way.


Solution

  • This is the PowerMockito way.

        @Test
        public void testCallPrivateMethod() throws Exception {
            Point actual = Whitebox.invokeMethod(powerMockDemo, 
                "privateMethod", new Point(11, 11));
    
            assertThat(actual.getX(), is(12));
            assertThat(actual.getY(), is(12));
        }
    

    https://automationrhapsody.com/call-private-method-powermock/