2. Cronoprogramma
● Software testing e software checking
● Livelli di test
● Unit test / JUnit
● Codice testabile
● Dependency Inversion
● Friendly Classes
● Test-Driven Development
3. Software Testing
Venire a
conoscenza di
eventuali rischi
Verifica dei requisiti
espliciti ed impliciti
Valutazione della
Qualità
● Correttezza
● Affidabilità
● Robustezza
● Usabilità
● Efficienza
● Manutenibilità
● Portabilità
4. Software Checking
Attività che mira a valutare un attributo o un
comportamento di un sistema, ed a verificare
che questo soddisfi i requisiti.
Checking is the process of making evaluations by applying
algorithmic decision rules to specific observations of a product.
6. Test Correttezza
Livello Pro Contro
System
tests
alta confidenza se tutto OK lenti
poco indicativi in caso di fail
difficilmente automatizzabili
Unit tests veloci
molto indicativi in caso di fail
automatizzabili
confidenza relativa
12. JUnit Best Practices
● N model classes → N test classes
● 1 test case per funzionalità
Stack Test Cases
● test Empty Stack
● test Correct Order
● test Null Input
● test Pop When Empty
13. JUnit Best Practices
Un assert per test
case
Stack stack = new Stack();
stack.push("A");
assertFalse(stack.isEmpty());
assertEquals("A", stack.peek());
assertFalse(stack.isEmpty());
assertEquals("A", stack.pop());
assertTrue(stack.isEmpty());
15. JUnit Best Practices
Come testo un metodo privato?
● non testare
● reflection
● cambiare visibilità
● classe emergente
16. JUnit Best Practices
Nome Classe di Test
Stack StackTest
Stesso package ma diversa cartella
● src/it/units/inginf
o Stack.java
● test/it/units/inginf
o StackTest.java
18. JUnit Best Practices
Testare le eccezioni
@Test(expected=NullPointerException.class)
public void shouldThrowExceptionOnNullInput() {
Stack stack = new Stack();
stack.push(null);
}
23. Come scrivere codice non testabile
● Mescolare i new con la logica
● Codice nel costruttore
● Global state (es. singletons)
● Metodi statici
● Troppi condizionali
● Troppe responsabilità
31. Inversion of Control (IoC)
MovieFilter
+moviesDirectedBy(director)
<<Interface>>
MovieFinder
+ getAllMovies()
MovieFinderImpl<<crea>>
Assemblatore <<crea>>
<<inject>>
32. IoC: Dependency Injection
● Constructor Injection
o Dipendenze esplicite
● Setter Injection
o Dipendenze meno esplicite
o Comodo in classi esistenti
● Framework
o Guice
o Spring
o CDI (J2EE)
36. Mockito
Mock Lifecycle:
● Crea mock a partire da interfaccia
● Programma comportamento voluto
● Utilizzo indiretto
● Verifica dell’utilizzo
37. Mockito
Esempio
MovieFinder finder = mock(MovieFinder.class)
when(finder.findAll()).thenReturn(allMovies);
MovieFilter filter = new MovieFilter(finder);
Lister.moviesDirectedBy(“Martin Scorsese”);
verify(finder).findAll(); // verifica la chiamata
esempio…
40. Test Driven Development
Si sviluppa in 3 fasi:
RED scrivere un singolo test che fallisce
GREEN scrivere quanto basta per far passare i test
BLUE refactoring
Metodologia di
sviluppo