Back Forum Reply New

A MockExternalContext is not a ServletExternalContext

Hello Community,

I am extending a ViewState so that I can perform some file generation that my page rendering depends on.  Here's the code in my view state:Code:   private void runFormGen( final RequestControlContext requestControlContext ) {       ServletExternalContext externalContext = (ServletExternalContext) requestControlContext    .getExternalContext();       formGeneratorService.setAbsolutePathToOutputFiles( externalContext.getContext().getRealPath( quot;quot; ) );       String includeFile = formGeneratorService.generateForm( flowViewId );       requestControlContext.getFlowScope().put( quot;includeFilequot;, includeFile );       requestControlContext.getFlowScope().put( quot;decoratorquot;, quot;westwoodLayoutquot; );   }
The problem I'm having is that cast.  When I'm testing this, I'm not using an fromServletRequest, which I'm assuming tells the framework to generate a ServletExternalContext.  Instead, I'm using a MockfromServletRequest, which results in the RequestControlContext being an instance of MockExternalContext.  

The solution I have right now is to wrap the code above with an if( instanceof ) type instruction.  That strikes me as inelegant because my test seems to be invading my code.  I'm wondering if there is a better way.

Any help is appreciated.

Setup the MockRequestControlContext with a ServletExternalContext (setExternalContext) wrapping a MockfromServletRequest and MockfromServletResponse.

Erwin

Try this

MockfromServletRequest servletRequest =   new MockfromServletRequest(quot;GETquot;, lt;uclgt;);
MockfromServletResponse servletResponse = new MockfromServletResponse();
MockServletContext servletContext = new MockServletContext();

ServletExternalContext servletExternalContext = new ServletExternalContext(
servletContext, servletRequest, servletResponse);

MockRequestContext context = new MockRequestContext();
context.setExternalContext(servletExternalContext)  ;

This way you can make sure that the testing efforts do not step in to your source code

Hope it helps...

Smitha
¥
Back Forum Reply New