传递EasyMock.anyObject()时出现断言错误

时间:2016-09-06 06:55:42

标签: junit4 easymock

基于某些处理我有两个方法背靠背调用。当我尝试传递EasyMock.anyObject()时,两者都返回了一些事件。执行测试用例null时,值会进入方法参数。我是EasyMock的新手。有人可以看看我在做什么错误吗?

错误:

java.lang.AssertionError: 
  Unexpected method call usrAuthenticationViaFormAction.submit(null, null, null):
    usrAuthenticationViaFormAction.setCredentialsToFlowScope(): expected: 1, actual: 0
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:85)
    at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:94)
    at com.shc.ecom.usr.web.action.USRAuthenticationViaFormAction$$EnhancerByCGLIB$$d3989200.submit(<generated>)
    at com.shc.ecom.test.usr.TestloginlFlow.testWhenServiceIsNotNullSubmitAuthenticationFailure(TestloginlFlow.java:438)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

方法:

class UsrAuthenticationViaFormAction {

    public  Event submit( RequestContext context,  Credential credential, MessageContext messageContext) {

        //          if (!checkLoginTicketIfExists(context)) {
        //              return returnInvalidLoginTicketEvent(context, messageContext);
        //          }

        if (isRequestAskingForServiceTicket(context)) {
            return grantServiceTicket(context, credential);
        }

        return createTicketGrantingTicket(context, credential, messageContext);
    }
}

测试用例:

public void test(){
    usrAuthenticationViaFormAction = EasyMock.createMock("usrAuthenticationViaFormAction", USRAuthenticationViaFormAction.class);

    Event event = new Event(this, "success");
    try {
        EasyMock.expect(usrAuthenticationViaFormAction.isRequestAskingForServiceTicket((EasyMock.anyObject())))
                        .andReturn(false).anyTimes();
        EasyMock.expect(usrAuthenticationViaFormAction.createTicketGrantingTicket((EasyMock.anyObject()),
                        EasyMock.anyObject(), EasyMock.anyObject())).andReturn(event).anyTimes();
        EasyMock.expect(usrInitialFlowSetupAction.doExecute(EasyMock.anyObject())).andReturn(event);
        EasyMock.replay(usrInitialFlowSetupAction);
        EasyMock.expect(usrAuthenticationViaFormAction.setCredentialsToFlowScope()).andReturn(false);
        EasyMock.replay(usrAuthenticationViaFormAction);
        EasyMock.expect(usrAuthenticationViaFormAction.submit(EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject())).andReturn(new Event(this, "authenticationFailure")).anyTimes();
        EasyMock.replay(usrAuthenticationViaFormAction);
    }

1 个答案:

答案 0 :(得分:0)

要添加PKR的评论 - 上面提到的UsrAuthenticationViaFormAction.submit方法永远不会被Test类调用。

当你这样做时

 usrAuthenticationViaFormAction = EasyMock.createMock("usrAuthenticationViaFormAction", USRAuthenticationViaFormAction.class);

usrAuthenticationViaFormAction被分配了一个模拟对象。这是EasyMock动态生成的类,没有真正的实现。

随后当你做

 EasyMock.expect(usrAuthenticationViaFormAction.submit(EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject())).andReturn(new Event(this, "authenticationFailure")).anyTimes();

调用的submit方法位于 mock 对象上。这只是通知简单的模拟框架,您希望稍后在测试执行期间调用此方法。它实际上并没有调用真正的提交方法。