myfaces-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ken Weiner <kwei...@gmail.com>
Subject Re: writing cactus tests for MyFaces
Date Tue, 04 Oct 2005 21:28:54 GMT
The test I'm trying to write is going to setup the HtmlSelectManyCheckbox
component, call encodeBegin(), encodeEnd() on it and then assert that the
componenet generated the correct markup. My understanding is that the
component will write to a javax.faces.context.ResponseWriter that it gets
from the FacesContext. Therefore, I need to stuff a MockResponseWriter into
the FacesContext before asking the component to render. Is there already a
mock ResponseWriter written for testing purposes? Is there another way I
should be doing things?

Thanks, Ken

On 10/4/05, Bill Dudney <bdudney@mac.com> wrote:
>
> Hi Ken,
> Thanks for writing tests!
>
> OK - first if you know JUnit you know about 80% of what you need to know
> to write cactus tests so you are at a good starting point.
>
> In cactus you have the same testXXX methods that you have in JUnit.
> However the actual test is run on the server. The 'how it works' page here;
>
> http://jakarta.apache.org/cactus/how_it_works.html
>
> does a great job of explaining the basics of how cactus works compared to
> JUnit.
>
> While writing tests for MyFaces components you should subclass from
> ServletTestCase and create your tests. Keep in mind that the beginXXX and
> endXXX execute on the client side as a way for you to setup the request (
> i.e. beginXXX) and then assert the look of the returned HTML (i.e.
> endXXX). As an example there is a LifecycleImplCactus test that implements
> beginXXX to simulate a JSP request to the faces servlet, then the test uses
> that to setup the correct environment, runs the test and asserts the correct
> stuff happened in the lifecycle (through a test only impl of the
> PhaseListener interface).
>
> So for a component test you can do something like this
>
> public void beginEncodeEnd(WebRequest request) {
> request.addParameter("form1:number1", "100.4");
> request.setURL("localhost:8080", "/test-app", "/faces", "/index.jsp",
> null);
> }
>
> public void testExecute() throws Exception {
> // setup a component to test
> UIViewRoot root = facesContext.getViewRoot();
> UIForm form = new UIForm();
> form.setId("form1");
> root.addForm(new UIForm()); // needs more detail but hopefully you get the
> idea
> MYComponent comp = new MYComponent();
> comp.setId("number1");
> form.addChild(comp);
> // simulate that this is the 2nd request for this page
> // so that we will go through all 6 phases
> // this will cause the viewRoot to be serialized for the service method
> // below to find
> StateManager stateManager = facesContext.getApplication()
> .getStateManager();
> stateManager.saveSerializedView(facesContext);
> // this will push the impl through all 6 phases
> servlet.service(this.request, this.response);
> }
>
> public void endEncodeEnd(WebResponse response) throws Exception {
> HTMLElement element = response.getElementWithID("form1:number1");
> assertEquals("100.4", element.getText());
> }
>
>
> Then in your setUp method;
>
> protected void setUp() throws Exception {
> super.setUp();
> servlet = new FacesServlet();
> servlet.init(this.config);
> // create a facesContext so you can simulate the first request
> FacesContextFactory facesContextFactory = (FacesContextFactory)
> FactoryFinder
> .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
> facesContext = facesContextFactory.getFacesContext(this.config
> .getServletContext(), request, response, lifecycle);
> }
>
> HTH,
>
> I'll try to get the meat of this posted to the wiki soon.
>
> Also this is pseudo code off the top of my head, don't expect it to work
> copy/paste
>
> You can see an example in the LifecycleImplCactus on the 1.1.1 branch.
>
> TTFN,
>
> -bd-
>
> On Oct 3, 2005, at 7:10 PM, Ken Weiner wrote:
>
> Hello, I've been asked to write some cactus tests for
> http://issues.apache.org/jira/browse/MYFACES-622 before my patch is
> applied.
>
> I am familiar with JUnit and even JWebUnit, but new to Cactus. It would
> help me if someone could provide some tips on getting started with writing a
> Cactus test for MyFaces. I see that there is one called
> HtmlDateRenderCactus, but I'm not sure what I need to setup before I try to
> invoke it.
>
> Thanks,
>
> Ken
>
>
>

Mime
View raw message