如何从 JMockit 模拟静态方法 [英] How to mock a static method from JMockit
本文介绍了如何从 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;
}
}
我想通过模拟 mockMethod 为方法 methodToTest 编写测试用例.如下尝试,它没有给出任何输出
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() );
}
推荐答案
模拟你的静态方法:
new MockUp<MyClass>()
{
@Mock
boolean mockMethod( String input ) // no access modifier required
{
return true;
}
};
这篇关于如何从 JMockit 模拟静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文