Buongiorno a tutti,
spero possiate aiutarmi... sto imparando a fare i test con JUnit ma ho molta confusione,al momento sto facendo un test sull'inserimento dei dati all'interno di un database Utente.
Allora il codice è questo:
import static org.junit.Assert.*;
import org.hibernate.cfg.Configuration;
import java.util.List;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import test.hibernate.Utente;
public class TestUtente {
private SessionFactory sessionFactory;
private org.hibernate.classic.Session session;
private Utente utente;
@Before
public void setUp() throws Exception {
sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
utente = new Utente();
utente.setNome("Prova");
session.beginTransaction();
// session.getTransaction().commit();
}
@After
public void tearDown() throws Exception {
// session.beginTransaction();
session.delete(utente);
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
@Test
//questo test Funziona (OK)
public void testF() {
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save( new Utente( "dat","dat", "dat","dat","dat" ) );
session.close();
session = sessionFactory.openSession();
session.beginTransaction();
List<Utente> result = session.createQuery( "from Utente " ).list();
for ( Utente utente : (List<Utente>) result ) {
System.out.println("Utente: " + utente.getCognome()+ " "+utente.getId()+""+utente.getNome()+");
}
session.getTransaction().commit();
session.close();
}
@Test
//questa è il test che NON FUNZIONA
public void testnotworking() {
//Assert.assertEquals(2, actual)
boolean firstTest ;
session = sessionFactory.openSession();
session.beginTransaction();
List<Utente> result = session.createQuery( "select nome from Utente where id=4" ).list();
riga 74) for ( Utente utente : (List<Utente>) result ) {
Assert.assertEquals("Prova", result);
System.out.println("Utente: " + utente.getNome()+ "");
}
session.getTransaction().commit();
session.close();
}
public static void main (String arg[]) throws Exception{
TestUtente test= new TestUtente();
test.setUp();
test.testF();
test.tearDown();
test.testnotworking();
}
}
l'errore che mi da è :
java.lang.ClassCastException:java.lang.String cannot be cast to test.hibernate.Utente
=at TestUtente.testnotworking(testUtentejava:74)
sapete dirmi che sto sbagliando? grazie