4 Replies Latest reply on Oct 20, 2011 10:32 AM by mokelet

    How can I test a Servlet?

    mokelet

      Hi,

       

      I have trouble with a Flash component which include by a p:media (Primefaces). This component itself is not testable, but it generate a post request and send it to a servlet. How can I simulate this request? I didn't found a solution with JSFUnit or HTMLUnit.

       

      ty

      Nico

        • 1. Re: How can I test a Servlet?
          ssilvert

          You can generate any kind of post request with HtmlUnit.  If you know what the request is going to look like then it is just a matter of using the HtmlUnit API to generate it.  If you have questionis about HtmlUnit then please direct it to their mailing list.

           

          Also, here is some doc about using HtmlUnit and JSFUnit together:

          http://community.jboss.org/wiki/UsingTheHtmlUnitAPIWithJSFUnit

           

          Stan

          • 2. Re: How can I test a Servlet?
            mokelet

            Hi Stan,

             

            I have greate a test which send a POST-request. It's look like this:

             

             

            @Test
            @InitialPage("/path/to/page.xhtml")
            public void testServlet (JSFServerSession server, JSFClientSession client) throws IOException {
            
            
            
            
            final WebClient webClient = new WebClient();
            
            WebRequest requestSettings = new WebRequest(new URL("http://localhost:8080/pathTo/Servlet"), HttpMethod.POST);
            ArrayList<NameValuePair> requestParameters = new  ArrayList<NameValuePair>();
            requestParameters.add(new NameValuePair("A", "1"));
            requestParameters.add(new NameValuePair("B", "2"));
            requestParameters.add(new NameValuePair("C", "3"));
            
            
            
            requestSettings.setRequestParameters(requestParameters);
            
            
            
            
            HtmlPage page = webClient.getPage(requestSettings);
            
            
            
            
            System.out.println(page.getWebResponse().getContentAsString());
            Assert.assertTrue(true);
            
            
            }
            
            
            

             

            The output is: &searchResult=3&invokeJS=showSearchResults (That's a generated responsetext).

             

            I think the problem is, that there is no link between initial page and webrequest. I was going to use the Webclient from JSFClientSession, but this Attribute is private. Have you a solution for this problem?

             

            big thx!

            • 3. Re: How can I test a Servlet?
              ssilvert

              Nico Knabe wrote:

               

              I think the problem is, that there is no link between initial page and webrequest. I was going to use the Webclient from JSFClientSession, but this Attribute is private. Have you a solution for this problem?

              Yes.  You get the WebClient from the JSFSession object. 

               

              @Test
              @InitialPage("/path/to/page.xhtml")
              public void testServlet (JSFSession jsfSession, JSFServerSession server, JSFClientSession client) throws IOException {
                   WebClient webClient =  jsfSession.getWebClient();
              

               

              Stan

              • 4. Re: How can I test a Servlet?
                mokelet

                Nice to know, that this signature is possible.

                But if I use the jsfsession.webclient, I get the same result ...

                And after the webClient.getPage command, the line "System.out.println(page.asText());" returned "&searchResult=3&invokeJS=showSearchResults", too.

                With server.getManagedBeanValue() I see that nothing changed.