io non so come spiegartele le cose, davvero.
Allora tu hai un metodo ad esempio
devi accertare che qualsiasi idUser tu hai in input, hai il giusto valore di ritorno.codice:public List getUser (String idUser) throws QualcheException;
Il tuo metodo deve:
1. ritornare una lista vuota (più logico di null per tanti motivi) se idUser non esiste nel db
2. ritornare la lista con un elemento se la persona è nel db
3. lanciare QualcheException se si verifica un problema.
Il tuo metodo ad esempio èOra il tuo test deve fare in modo che qualsiasi id tu dai hai un risultato coerente.Codice PHP:public List getUser (String idUser) throws QualcheException{
List result = new ArrayList();
String query = "SELECT * FROM Persone WHERE id = \"" + idUser + "\";
//ti connetti al db, esegui la query e poi hai valorizzi la lista di return
// result = eseguiQuery( query );
return result;
}
Ma il tuo test chiama il metodo che fa la query, non devi riportare dentro il test le cose che fai dentro il metodo!!!
Es.
questo è il modo di scrivere una funzione e di usare gli unit test.Codice PHP:
public void testUserNotOk(){
String id = "6";
// ti aspetti vuoto
List temp = getUser(id);
assertNotEquals(temp, null);
assertEquals(temp.size(), 0);
}
public void testUserOk(){
String id = "5";
// ti aspetti insieme valido
List temp = getUser(id);
assertNotEquals(temp, null);
assertNotEquals(temp.size(), 0);
assertEquals(temp.size(), 1);
assertEquals(temp.get(0).getNome(), "PROVA");
}
public void testUserNotOk(){
String id = null;
// ti aspetti eccezione
try{
List temp = getUser(id);
}catch(QualcheException ex){
assertEquals(ex.getMessage(), "Messaggio generato dall'eccezione") ;
}
// se arrivi qui è fail, non è previsto
}

Rispondi quotando