java使用junit4,使用Junit4和Hibernate在Java中测试DAO

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值