EasyMockError

146 views
Skip to first unread message

Santhosh Kumar

unread,
Feb 24, 2015, 9:13:20 AM2/24/15
to powe...@googlegroups.com
Hello All,

I am new to jUnits and Easy Mock. I have the following code

public void testHandleCreateCustomer() throws EFilialeManagerException, ServletException, IOException
     
    SCRegisterHandler handler2  =  (SCRegisterHandler) nucleus.resolveName("/de/dpag/efiliale/servicecenter/SCRegisterHandler");
  
    try {
        System.out.println("handler2::::::: " +handler2);
        assertNotNull(handler2);
     
     
        DropletInvoker invoker = new DropletInvoker(nucleus);
        DynamoHttpServletRequest request = EasyMock.createMock(DynamoHttpServletRequest.class);
        EasyMock.expect(request.setParameter("username").andReturn("dpsampleusername"));
        EasyMock.expect(request.setParameter("salutation").andReturn("Herr"));
        EasyMock.expect(request.setParameter("title").andReturn("Mr"));
        EasyMock.expect(request.setParameter("givenname").andReturn("DPFirst"));
        EasyMock.expect(request.setParameter("surname").andReturn("DPLast"));
        EasyMock.expect(request.setParameter("ekpNumber").andReturn("321"));
        EasyMock.expect(request.setParameter("salutation").andReturn("Herr"));
        EasyMock.expect(request.setParameter("kundengruppe").andReturn("DPSampleCustomerGroup"));
        EasyMock.expect(request.setParameter("location").andReturn("Berlin"));
        EasyMock.expect(request.setParameter("zip").andReturn("53173"));
        EasyMock.expect(request.setParameter("streetno").andReturn("35"));
        EasyMock.expect(request.setParameter("streetName").andReturn("sampleDPStreet"));
        EasyMock.expect(request.setParameter("companyName").andReturn("sampleDPcompany"));
        EasyMock.expect(request.setParameter("emailaddress").andReturn("sample...@gmail.com"));
       
        DynamoHttpServletResponse res = mServletTestUtils.createDynamoHttpServletResponse();
        handler2.handleCreateCustomer(request, res);
       
       
        if(handler2 != null){
            @SuppressWarnings("rawtypes")
            Class classRef = handler2.getClass();
            Method method;
         
                method = classRef.getDeclaredMethod("handleCreateCustomer",(Class[])null);
                method.setAccessible(true);
                Object object = method.invoke(handler2, (Object[])null);
               assertNotNull(object);
              }
    } catch (SecurityException exception) {
        LogFacade.error(LogFacade.BUSINESS, "Test Case Failed - SecurityException", exception);
    } catch (NoSuchMethodException exception) {
        LogFacade.error(LogFacade.BUSINESS, "Test Case Failed - NoSuchMethodException", exception);
    } catch (IllegalArgumentException exception) {
        LogFacade.error(LogFacade.BUSINESS, "Test Case Failed - IllegalArgumentException", exception);
    } catch (IllegalAccessException exception) {
        LogFacade.error(LogFacade.BUSINESS, "Test Case Failed - IllegalAccessException", exception);
    } catch (InvocationTargetException exception) {
        LogFacade.error(LogFacade.BUSINESS, "Test Case Failed - InvocationTargetException", exception);
    }
}

when I execute the tests, I get the following error.

java.lang.IllegalStateException: missing behavior definition for the preceding method call:
DynamoHttpServletRequest.getParameter("salutation")
Usage is: expect(a.foo()).andXXX()
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:42)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:85)
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:94)
at atg.servlet.DynamoHttpServletRequest$$EnhancerByCGLIB$$8c76eb09.getParameter(<generated>)
at de.dpag.efiliale.servicecenter.SCRegisterHandler.createCustomer(SCRegisterHandler.java:533)
at de.dpag.efiliale.servicecenter.SCRegisterHandler.handleCreateCustomer(SCRegisterHandler.java:768)
at de.dpag.efiliale.servicecenter.SCRegisterHandlerTest.testHandleCreateCustomer(SCRegisterHandlerTest.java:163)
at junit.extensions.TestDecorator.basicRun(TestDecorator.java:24)
at junit.extensions.TestSetup$1.protect(TestSetup.java:23)
at junit.extensions.TestSetup.run(TestSetup.java:27)


Please help

Thanks & Regards,
Santhosh

ರಂಜಿತ್ ಕುಮರ್

unread,
Feb 25, 2015, 8:17:29 PM2/25/15
to powe...@googlegroups.com
Hi Santhosh,

I think you suppose to use get methods, instead set methods while mocking, because set method are void method and it wont return any thing.

Examaple
   EasyMock.expect(request.setParameter("username").andReturn("dpsampleusername"));

instead of this you suppose to use

   EasyMock.expect(request.getParameter("username").andReturn("dpsampleusername"));


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards
Ranjith Kumar M A

vincenzo dc

unread,
May 12, 2016, 9:17:55 AM5/12/16
to PowerMock
You can't use Mock Object to save information, so you can't call expect() on set methods.
Mock object are used to simulate their behaviour when are used in other methods, so you don't need a real object but you pass a mock oject that you can control as you like, using  expect(obj.method1()).andstubReturn("yourDesiredReturn")
Reply all
Reply to author
Forward
0 new messages