Visualizzazione dei risultati da 1 a 3 su 3

Discussione: JUnit

  1. #1

    JUnit

    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

  2. #2
    Ho capito l'errore devo modificare la list in String

  3. #3
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,472

    Moderazione

    Originariamente inviato da Marisascar
    Ho capito l'errore devo modificare la list in String
    In futuro, usa il tag [CODE] (come descritto nel Regolamento) per formattare il codice sorgente riportato.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.