0

I just want to setAttribute from the HttPServletRequest. Here's the code snippet for the 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;
    }};

This result of this line: req.setAttribute(ConfigurationConstant.GROUPS, groups); result = requestAttributeDelegate; never work with the delegated method.

This is the error message prompted:

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)

What's wrong with this???

lannyboy
  • 617
  • 1
  • 10
  • 20

1 Answers1

0

found the problem:

    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) At first, setAttribute sequence was wrongly inserted. It has to be on top of req.getRequestDispatcher line.
2) The delegate method must remove the getAttribute method.

lannyboy
  • 617
  • 1
  • 10
  • 20