如何使用withInstanceOf使用JMockit模拟私有方法?

时间:2014-07-08 14:08:52

标签: unit-testing junit stub jmockit stubbing

我需要知道如何在JMockit中使用withInstanceOf来模拟私有方法? 这是我需要测试的类。 package mockingPrivateMethodWithInstanceOf;

public class Simple
{
    private String iAmPrivate(String argString) 
    {
        return "Private Method";
    }
    public String publicCallsPrivate() 
    {
        String string = "aString";
        return iAmPrivate(string);
    }
}

这是测试类

public class SimpleTest 
{    
    @Test 
    public void testPublicInvokesPrivate()
    {     
        final Simple simple =  new Simple();      
        new Expectations(simple)
        {
            {
                Deencapsulation.invoke(simple, "iAmPrivate", withInstanceOf(String.class));
                returns("Mocked method");
            }
        };
        assertEquals("Mocked method", simple.publicCallsPrivate());
    }   
}

执行测试类时,它说:

java.lang.IllegalArgumentException: Invalid null value passed as argument 0

0 个答案:

没有答案