-1

As per my knowledge, We can Mock the private method in same class by using PowerMockito.

With in the same class is working fine for me , but when i'm calling private method from the other class it's not working.

Below Example i've 2 classes , Service class and Helper classes

Helper class having private method.

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ Helper.class,Service.class })
    @PowerMockIgnore("javax.management.*")
    public class EPartnerBatchServiceTest {

        private Helper helper;

        @InjectMocks
        private ServiceClass serviceClass;

        @Before
        public void setUp() throws Exception {
            helper = PowerMockito.spy(new Helper());
            ServiceClass = PowerMockito.spy(new serviceClass());
            MockitoAnnotations.initMocks(this);
        }

        @Test
        public void testUpdateIndividualUserStatus() throws Exception {

            PowerMockito.doReturn("Test").when(helper, "privateMethod", anyString(), Matchers.anyObject());

            String response = serviceClass.update(loggerId, activityLogDTO);

        }
    }

Sample Classes :

Class A{
    value=new B().method1();
}

Class B{
    public method1(){
     value = method2();   
    }

    private method2(){
     return "Test";   
    }
}
Praveen
  • 3
  • 7
  • Possible duplicate of [Mock private method using PowerMockito](https://stackoverflow.com/questions/28121177/mock-private-method-using-powermockito) – Mohamed Sweelam Nov 20 '19 at 10:46
  • Show the two classes definitions. The question in its current state is incomplete and therefore unclear. – Nkosi Nov 20 '19 at 10:54
  • Its not exact Test calss, I've modified for simple understanding. Please find the sample code i've added for more clarification. – Praveen Nov 20 '19 at 11:14
  • Mohamed Sweelam : What you said is correct , but it is usefull to mock the private method , in the same class only . i want to mock private () In other class – Praveen Nov 20 '19 at 11:26
  • Could probably do with a more complete example but if class "A" is doing "new B()" you're probably missing "PowerMockito.whenNew(B.class).withNoArguments().thenReturn(someSpyBInstance);" from your test. – John Stringer Nov 20 '19 at 16:53
  • John Stringer : Thank so much bro. PowerMockito.whenNew(B.class).withNoArguments().thenReturn(someSpyBInstance); its working for me. Thank you!! – Praveen Nov 21 '19 at 10:14

1 Answers1

0

You shouldn't be worrying with testing explicitly your private methods, since they are not accessible for the ones calling it, it's function should be tested somewhere in the flow of your public methods. But, if for some reason you need to test them explicitly, then maybe reflections and setting those methods as accessible for testing may resolve your problem.

You'll find great examples here: https://www.baeldung.com/java-method-reflection

Magalhães
  • 21
  • 4
  • 1
    Thanq for your Help. In my case below code is working for me. PowerMockito.whenNew(B.class).withNoArguments().thenReturn(someSpyBInstance); – Praveen Nov 21 '19 at 10:16
  • I'm glad i've been able to help, about the point i've talked about over not worrying over private methods, this is not a statement written in stone. Even where i work we often discuss the topic and try to see a better way for each case we talk about. From the point of view of unit testing, i think that exists a good logic in testing every little unit of your code, so... it's actually up to you on what your testing, why and when. – Magalhães Nov 27 '19 at 13:18