简体   繁体   中英

How to mock a static method from JMockit

I have a static method which will be invoking from test method in a class as bellow

public class MyClass
{
   private static boolean mockMethod( String input )
    {
       boolean value;
       //do something to value
       return value; 
    }

    public static boolean methodToTest()
    {
       boolean getVal = mockMethod( "input" );
       //do something to getVal
       return getVal; 
    }
}

I want to write a test case for method methodToTest by mocking mockMethod . Tried as bellow and it doesn't give any output

@Before
public void init()
{
    Mockit.setUpMock( MyClass.class, MyClassMocked.class );
}

public static class MyClassMocked extends MockUp<MyClass>
{
    @Mock
    private static boolean mockMethod( String input )
    {
        return true;
    }
}

@Test
public void testMethodToTest()
{
    assertTrue( ( MyClass.methodToTest() );
} 

To mock your static method:

new MockUp<MyClass>()
{
    @Mock
    boolean mockMethod( String input ) // no access modifier required
    {
        return true; 
    }
};

To mock the static private method:

@Mocked({"mockMethod"})
MyClass myClass;

String result;

@Before
public void init()
{
    new Expectations(myClass)
    {
        {
            invoke(MyClass.class, "mockMethod", anyString);
            returns(result);
        }
    }
}

@Test
public void testMethodToTest()
{
    result = "true"; // Replace result with what you want to test...
    assertTrue( ( MyClass.methodToTest() );
} 

From JavaDoc:

Object mockit.Invocations.invoke(Class methodOwner, String methodName, Object... methodArgs)

Specifies an expected invocation to a given static method, with a given list of arguments.

There is another way of mocking static methods using JMockit (using Delegate class). I find it more convenient and elegant.

public class Service {
  public String addSuffix(String str) { // method to be tested
    return Utils.staticMethod(str);
  }
}

public class Utils {
  public static String staticMethod(String s) { // method to be mocked
    String suffix = DatabaseManager.findSuffix("default_suffix");
    return s.concat(suffix);
  }
}

public class Test {

  @Tested
  Service service;

  @Mocked
  Utils utils; // @Mocked will make sure all methods will be mocked (including static methods)

  @Test
  public void test() {
    new Expectations {{
      Utils.staticMethod(anyString); times = 1; result = new Delegate() {
        public static String staticMethod(String s) { // should have the same signature (method name and parameters) as Utils#staticMethod
          return ""; // provide custom implementation for your Utils#staticMethod
        }
      }
    }}
    
    service.addSuffix("test_value");

    new Verifications {{
      String s;
      Utils.staticMethod(s = withCapture()); times = 1;
      assertEquals("test_value", s); // assert that Service#addSuffix propagated "test_value" to Utils#staticMethod
    }}
  }
}

Reference:

https://jmockit.github.io/tutorial/Mocking.html#delegates https://jmockit.github.io/tutorial/Mocking.html#withCapture

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM