getting error : this method must not be called in replay state

6,147 views
Skip to first unread message

Lavnish Lalchandani

unread,
May 14, 2009, 9:21:49 AM5/14/09
to powe...@googlegroups.com
Hi
I need to write a junit test case just for purpose of "code coverage". I know i am not really testing anything here.

here is the code that i need to mock

     public static Object invokeEL(String el, Class[] paramTypes,
                                   Object[] params)
     {
         theLogger.entering("TemplateBean","invokeEL");
//         FacesContext facesContext = FacesContext.getCurrentInstance();
//         ELContext elContext = facesContext.getELContext();
//         Application app = facesContext.getApplication();
//         ExpressionFactory expressionFactory =app.getExpressionFactory();
//         MethodExpression exp =expressionFactory.createMethodExpression(elContext, el,Object.class, paramTypes);
         theLogger.exiting("TemplateBean","invokeEL");
//         Object retVal = exp.invoke(elContext, params);
         return null;
     }

here is my test case

   public void testInvokeEL()
   {
      FacesContext mockFC =
         PowerMock.createMock(FacesContext.class); //System.out.println("testRefreshPage 2");
      PowerMock.mockStatic(FacesContext.class);
      ELContext mockEC = PowerMock.createMock(ELContext.class);
      Application mockAPP = PowerMock.createMock(Application.class);
      ExpressionFactory mockEF = PowerMock.createMock(ExpressionFactory.class);
      MethodExpression mockME = PowerMock.createMock(MethodExpression.class);
      Object retObj = new Object();              

//      org.easymock.EasyMock.expect(FacesContext.getCurrentInstance()).andReturn(mockFC);
//      org.easymock.EasyMock.expect(mockFC.getELContext()).andReturn(mockEC);
//      org.easymock.EasyMock.expect(mockFC.getApplication()).andReturn(mockAPP);
//      org.easymock.EasyMock.expect(mockAPP.getExpressionFactory()).andReturn(mockEF);
//      org.easymock.EasyMock.expect(mockEF.createMethodExpression(mockEC,"testInvokeEL",Object.class,null)).andReturn(mockME);
//      org.easymock.EasyMock.expect(mockME.invoke(mockEC,null)).andReturn(retObj);
      PowerMock.replayAll();
      Object temp = TemplateBean.invokeEL("testInvokeEL",null,null); // this is line no 289 , where i get error
      PowerMock.verifyAll();
}

I get the error with either lines commented or uncommented
I get exception

java.lang.IllegalStateException : This method must not be called in replay state
org.easymock.internal.MocksControl.replay(mocksControl.java:91)
org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.replay(EasyMockMethodInvocationControl:104)
org.powermock.api.easymock.PowerMock.replay(PowerMock:1573)
org.powermock.api.easymock.PowerMock.replayAll(PowerMock:1438)
mypackage.QATemplateBean.testinvokeEL(QATemplateBean:289)

I dont understand what the error is and why i am getting it ?
will appreciate any help.
error.JPG

Johan Haleby

unread,
May 14, 2009, 9:52:52 AM5/14/09
to powe...@googlegroups.com
Hmm that seems strange indeed, looks like replayAll is called twice for some reason. I can imagine that has something to do with that you're mixing the test case and the actual object you want to test in the same class (don't you?). Maybe the byte-code manipulation doesn't work correctly in these cases, I've never tried it anyways. If this is the case you could (and probably should) try to move the code you'd like to test to a seperate class. I'll see if I can reproduce it asap.

/Johan

Johan Haleby

unread,
May 14, 2009, 10:35:51 AM5/14/09
to powe...@googlegroups.com
I fail to reproduce it. If possible please attach some source code that I can try for myself.

/Johan
Reply all
Reply to author
Forward
0 new messages