1

Testing DAO'm using junit and mainly what I do is start a transaction in hibernate, call the compare function, and then roll back the transaccion.El problem is that if I'm Testing function is a transaction error I can not strip nest, I thought about the idea of ​​implementing DBUnit xml but management does not seem a good idea and less in this instance that I did enough test cases with this method. Anyone have any idea how to fix it without having to use anything other than Junit or hibernate?

This is a example

@Test
public void Test1GetByCodigo(){
    String cod = "999999999";
    DBTenant dbTenant = null; 
    Session sess = null;
    Transaction trans = null;
    ClienteBO cli = null;
    Clientes clie = null;
    try{
        try{
            dbTenant = new DBTenant();
            sess = dbTenant.getTenantSession();
            trans = sess.beginTransaction();
        }
        catch(Exception e){
            fail("Error en la carga de la transaccion.Quedo alguna transaccion abierta?");
        }   
        clie  = CargaCliente(cod);
        sess.save(clie);
        cli = cliBL.getByCodigo(cod);
    }
    catch(Exception e){
        trans.rollback();
        dbTenant.closeSession();
        fail("Error la carga del cliente.Se modifico la bse de datos Clientes??");
    }
    trans.rollback();
    sess.clear();
    dbTenant.closeSession();
    ClienteBO clieEsp = CargaClienteBO(clie);
    assertNotNull(clieEsp);
    assertNotNull(cli);
    assertEquals("Error el cliente no coincide",clieEsp,cli);
}

if cliBL.getbycodigo function () tubiera a transaction would have a bug, thank you for your help thanks

3
  • if cliBL.getbycodigo function () have a transaction would have a bug, thank you for your help thanks Commented Oct 16, 2012 at 13:41
  • I'm using HibernateUtil to create the SessionFactory. The HibernateUtil is working fine during normal run of the application, but it's giving ExceptionInInitializer error when I'm trying to run JUnit4 tests. Please help. Commented Dec 29, 2012 at 10:23
  • Can you please share your DBTenant code. Commented Dec 29, 2012 at 10:23

1 Answer 1

0

Set up an inmemory database for this test using hibernate and let the junit test use transactions as much as it likes. because the database is created before the test and thrown away afterwards there is no problem with isolation. This article might help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.