繁体   English   中英

jmockit返回相同的对象

[英]jmockit return the same object

我可以使用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);
        }
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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