Search code examples
javamockingjmockit

Is there any way to invoke method from nested class by jmockit?


I have the following class:

public class SomeClass1
{
   protected void method1()
   {
      String someString = NestedClass1.subMethod1("Hi");
   }

   private static class NestedClass1
   {
      static String subMethod1(String param1)
      {
          return param1;
      }
   } 
}

I mock the method1 of SomeClass1 in my test and I need invoke the NestedClass1.subMethod1 in body of mocked method1 as in the original. I have tried utility methods from the Deencapsulation class (invoke, newInstance, newInnerInstace), but without desired result.

 new MockUp<SomeClass1>()
 {
     @Mock
     void method1()
     {

     }
 };

Any idea to solve this problem? Thanks in advance.


Solution

  • You have to use the correct class name (as String) for a static inner class:

    Deencapsulation.invoke("org.yourpackage.SomeClass1$NestedClass1", "subMethod1", "Hello");
    

    Notice the "$" sign