委托方法不适用于Jmockit单元测试

时间:2014-04-10 11:03:30

标签: unit-testing junit jmockit

我只想从HttPServletRequest中设置setAttribute。这是Jmockit的代码片段。

    new Expectations() {
        private final Delegate requestAttributeDelegate = new Delegate() {

            final Map<String, Object> attributes = new HashMap<String, Object>(); 

            Object getAttribute(String key) { 
                return attributes.get(key);
            } 

            void setAttribute(String key, Object value) { 
                attributes.put(key, value); 
            } 
        }; {
        req.getParameter(ParameterConstant.ACTION); result = ActionConstant.REMOVE;
        req.getParameter(ParameterConstant.EMAIL); result = myEMail;
        req.getRequestDispatcher(PageConstant.USER_REMOVE_GROUP_FORM); result = dispatcher;
        req.setAttribute(ConfigurationConstant.GROUPS, groups); result = requestAttributeDelegate;
        dispatcher.forward(req, resp); times = 1;
    }};

此行的结果:req.setAttribute(ConfigurationConstant.GROUPS, groups); result = requestAttributeDelegate;永远不会使用委派方法。

这是提示的错误消息:

java.lang.IllegalArgumentException: More than one non-private invocation handler method found
    at com.company.cisco.actionitems.test.UserTest$16.<init>(UserTest.java:371)
    at com.company.cisco.actionitems.test.UserTest.Test013_ActionRemove_DoGet_Case01(UserTest.java:355)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

这有什么问题???

1 个答案:

答案 0 :(得分:0)

发现了问题:

    new Expectations() {
        private final Delegate requestSetAttributeDelegate = new Delegate() {

            final Map<String, Object> attributes = new HashMap<String, Object>(); 

            void setAttribute(String key, Object value) { 
                attributes.put(key, value); 
            } 
        }; {
        req.getParameter(ParameterConstant.ACTION); result = ActionConstant.REMOVE;
        req.getParameter(ParameterConstant.EMAIL); result = myEMail;
        req.setAttribute(ConfigurationConstant.GROUPS, groups); result = requestSetAttributeDelegate;
        req.getRequestDispatcher(PageConstant.USER_REMOVE_GROUP_FORM); result = dispatcher;
        dispatcher.forward(req, resp); times = 1;
    }};

1)首先,错误地插入了setAttribute序列。它必须位于req.getRequestDispatcher行之上。
2)委托方法必须删除getAttribute方法。