-1

I am writing junit test to test the private method which has class declared in it.

My code:

private Object getFunctReturn()
 {
   InterfaceName obj = new ClassName():
   Map map = obj.getMap();
   map.foreach()->{
            //iterating map here....
   }
 }

I am using WhiteBox api to test private methods. How can I mock InterfaceName obj = new ClassName(); so that i can proceed to for next execution of code?

James Z
  • 12,209
  • 10
  • 24
  • 44
TechP88
  • 25
  • 1
  • 6
  • 1
    Why would you want to mock it? You should only test the observable side effects. And you should not test a `private` method in the first place. – luk2302 Jul 10 '21 at 12:34

1 Answers1

0

Try like this:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassUnderTest.class)
public class SimpleTest {

    @Test
    public void test() throws Exception {
        ClassUnderTest underTest = new ClassUnderTest();

        ClassName obj = new ClassName();

        PowerMockito.whenNew(ClassName.class).withNoArguments().thenReturn(obj);

        WhiteboxImpl.invokeMethod(underTest, "getFunctReturn");
    }
}
luk2302
  • 55,258
  • 23
  • 97
  • 137
Georgii Lvov
  • 1,771
  • 1
  • 3
  • 17
  • The ClassName implements org.eclipse.microprofile.cinfig.spi.ConfigSource interface which do not have setMap() method. – TechP88 Jul 11 '21 at 16:19
  • @TechP88 Removed the map call which was only there for demonstration purposes for you. – luk2302 Jul 12 '21 at 08:37