Il framework JUNIT Scaletta Cos'è JUnit? Test unitari e test ...
Il framework JUNIT Scaletta Cos'è JUnit? Test unitari e test ...
Il framework JUNIT Scaletta Cos'è JUnit? Test unitari e test ...
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Varianti di scrittura 1/2<br />
• È possibile inserire nel main della classe di <strong>test</strong> la<br />
chiamata al <strong>framework</strong> <strong>JUnit</strong>:<br />
class <strong>Test</strong>Importo …{ …<br />
public static void main(String[] args){<br />
junit.textui.<strong>Test</strong>Runner.run(suite());<br />
}<br />
…}<br />
class <strong>Test</strong>Importo …{ …<br />
public static void main(String[] args){<br />
junit.swingui.<strong>Test</strong>Runner.run(suite());<br />
}<br />
…}<br />
• È quindi possibile eseguire i <strong>test</strong> digitando semplicemente:<br />
java importo_<strong>test</strong>.<strong>Test</strong>Importo<br />
28/04/2004 <strong>JUNIT</strong> - Mauro Lorenzutti 19<br />
Failure ed Error<br />
• Si ha failure se i <strong>test</strong> di uguaglianza da noi<br />
impostati non vengono verificati.<br />
• Si ha un error quando durante i <strong>test</strong><br />
vengono sollevate delle eccezioni non<br />
gestite.<br />
• Come si può <strong>test</strong>are la corretta gestione di<br />
un’eccezione?<br />
28/04/2004 <strong>JUNIT</strong> - Mauro Lorenzutti 21<br />
Generiamo un’eccezione 2/2<br />
• Eseguiamo i <strong>test</strong> ed analizziamo l’output fornito da <strong>JUnit</strong>:<br />
.E<br />
Time: 0<br />
There was 1 error:<br />
1) <strong>test</strong>ToString(importo_<strong>test</strong>.<strong>Test</strong>Importo)java.lang.<br />
NumberFormatException<br />
at importo_progetto.Importo.(Importo.java:28)<br />
at<br />
importo_<strong>test</strong>.<strong>Test</strong>Importo.setUp(<strong>Test</strong>Importo.java:51)<br />
FAILURES!!!<br />
<strong>Test</strong>s run: 1, Failures: 0, Errors: 1<br />
• Si è verificato un error: l’eccezione è stata sollevata ma<br />
non gestita dalla classe di <strong>test</strong>.<br />
28/04/2004 <strong>JUNIT</strong> - Mauro Lorenzutti 23<br />
Varianti di scrittura 2/2<br />
• Non utilizzare la suite di <strong>test</strong>:<br />
– Nominare tutti i <strong>test</strong> da effettuare come<br />
<strong>test</strong>nomemetodo_da_<strong>test</strong>are<br />
– Non utilizzare suite, setUp e tearDown perché vengono ignorati<br />
– Eseguire normalmente (<strong>JUnit</strong> automaticamente <strong>test</strong>a tutti i metodi<br />
il cui nome rispetta la convenzione di cui sopra)<br />
public class <strong>Test</strong>Importo2 extends <strong>Test</strong>Case{<br />
public void <strong>test</strong>ToString(){<br />
Importo i1 = new Importo(true, 12, 16);<br />
Importo i2 = new Importo(false, 1, 99);<br />
assertEquals(i1.toString(), "12,16");<br />
assertEquals(i2.toString(), "-1,99");<br />
}<br />
28/04/2004 }<br />
<strong>JUNIT</strong> - Mauro Lorenzutti 20<br />
Generiamo un’eccezione 1/2<br />
• Modifichiamo il costruttore della classe da <strong>test</strong>are affinché<br />
generi una eccezione nel caso venga passato un numero di<br />
euro o di centesimi negativo:<br />
public Importo(boolean positivo, long euro, long cent){<br />
if (euro >= 0 && cent >= 0){<br />
this.positivo = positivo;<br />
this.euro = euro + (cent / 100);<br />
this.cent = cent % 100;}<br />
else{throw new NumberFormatException();}<br />
}<br />
• Modifichiamo la classe di <strong>test</strong> affinché verifichi questa<br />
condizione:<br />
public void setUp(){<br />
i1 = new Importo(true, -12, 16);<br />
28/04/2004 <strong>JUNIT</strong> - Mauro Lorenzutti 22<br />
i2 = new Importo(true, 1, 99);}<br />
<strong>Test</strong>iamo le eccezioni 1/5<br />
• Si utilizza il metodo fail che interrompe l’esecuzione dei<br />
<strong>test</strong> generando una failure e riporta in output la stringa<br />
passatagli come parametro.<br />
public void <strong>test</strong>CostruttoreNegativo(){<br />
try{<br />
Importo i3 = new Importo(false, -10, 15);<br />
fail("Deve generare una eccezione");<br />
}<br />
catch(NumberFormatException nfe){}<br />
}<br />
28/04/2004 <strong>JUNIT</strong> - Mauro Lorenzutti 24<br />
4