我可以以返回传递给它的参数的方式模拟使用jmockit的方法吗?
考虑这样的签名;
public String myFunction(String abc);
我看到可以使用Mockito进行此操作。但这在JMockit中可行吗?
jmockit手册提供了一些指导...我真的建议您阅读。您要寻找的构造可能看起来像这样:
@Test
public void delegatingInvocationsToACustomDelegate(@Mocked final DependencyAbc anyAbc)
{
new Expectations() {{
anyAbc.myFunction(any);
result = new Delegate() {
String aDelegateMethod(String s)
{
return s;
}
};
}};
// assuming doSomething() here invokes myFunction()...
new UnitUnderTest().doSomething();
}
jmockit也可以捕获参数:
@Test
public void testParmValue(@Mocked final Collaborator myMock) {
// Set the test expectation.
final String expected = "myExpectedParmValue";
// Execute the test.
myMock.myFunction(expected);
// Evaluate the parameter passed to myFunction.
new Verifications() {
{
String actual;
myMock.myFunction(actual = withCapture());
assertEquals("The parameter passed to myFunction is incorrect", expected, actual);
}
};
}