-
1. Re: How can I test a Servlet?
ssilvert Oct 16, 2011 4:17 PM (in response to mokelet)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 Oct 20, 2011 7:28 AM (in response to ssilvert)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 Oct 20, 2011 9:11 AM (in response to mokelet)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 Oct 20, 2011 10:32 AM (in response to ssilvert)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.