www.fabiobeta.it

« W i Repository!
Esperimenti con Vise (by Feathers) »

Ciao, sono Fabio.
Su queste pagine potete trovare esperienze, idee ed esperimenti di un programmatore in cerca di metodi per lavorare meglio e meno.

RSS

Questi sono i post più apprezzati del sito, dateci un'occhiata:

SFTP automatico e natalizio con ANT

La mia cassetta degli attrezzi: Il portatile

III: Tu non commenterai invano

YAGNI for Managers

Stand Up Meeting: Definizione

Eclipse: Propaganda a suon di keystroke

HtmlPreviewer

Gli altri sono qui.

httpunit: JSP Testing
19 Giugno 2006

Qualche mese fa passando sulle FAQ di httpUnit leggevo:

Can I use JSPs with ServletUnit?

Absolutely! But since you are now emulating the servlet environment, you will need to make sure that you have access to the relevant classes. By default, ServletUnit is configured to use Jasper, the JSP engine which is part of Tomcat. You will therefore need the Jasper jar in your classpath, along with any on which it depends. Once you have done that, your JSPs should run, just as they would in Tomcat.

Leggendo la FAQ avevo pensato "Maletedetto! almeno un esempio da farmi copiare ed incollare però potevi metterlo!!".

Visto che sia l'XPUG che il cliente del mio lavoro abituale stanno muovendosi con Java Servlet Pages, mi sono deciso a vedere se quell'Absolutely! non nascondesse qualche trappola. Persa una mezz'oretta a dar la caccia a file *.jar per soddisfare prerequisiti di librerie (per l'esempio servono le librerie di jasper e quelle di ant), ecco il codice:

helloworld.jsp, mollata in una cartella /jsp del progetto

<html><body>
hello world! I am a JSP! I can multiplicate numbers!
<br>2*2 = <%= 2*2 %>
</body></html>
La classe di test:
package it.fabiobeta.jspunit;


import junit.framework.TestCase;
import com.meterware.httpunit.*;
import com.meterware.servletunit.*;

public class JSPTest extends TestCase {

ServletRunner sr;

public JSPTest(String arg0) {
super(arg0);
sr = new ServletRunner();
}

public void testHelloworld() throws Exception {
ServletUnitClient sc = sr.newClient();
WebRequest request = new PostMethodWebRequest("http://quellochevuoi/jsp/helloworld.jsp");
WebResponse response sc.getResponse(request);
String responseText = response.getText();
assertNotNull("No response received", response);
assertEquals("content type", "text/html", response.getContentType());
assertTrue(responseText.indexOf("2*2 = 4") > -1);
}
}

Effettivamente funziona liscio liscio... però una mail con l'esempio a httpunit-develop@lists.sourceforge.net l'ho mandata ;)

send a feedback to fabio

« W i Repository!
Esperimenti con Vise (by Feathers) »

Fight Spam! Click Here!