开发者

Automatic test asserting that a property is really lazy with hibernate

开发者 https://www.devze.com 2023-01-17 22:44 出处:网络
I have to add a lazy=true to a property & i have add a ant task to intrument the class to enable hibernate to make it lazy in my pom.

I have to add a lazy=true to a property & i have add a ant task to intrument the class to enable hibernate to make it lazy in my pom.

is there a way to test that now this property is in lazy mode in runti开发者_运维问答me without watching the hibernate sql console output? I mean with unit testing (im using spring test with junit). I dont want to test that the lazy attribute is present in the xml mapping. This alone doesnt ensure taht hibernate will treat it as a lazy property.

I want a junit test in order to be sure that this bug revealed by a tester will not more appear in the future (i'm not alone in the project).

I'm following the (best?) practice that every not trivial bug should have an automatic test in order to prevent his coming back.(by the way, i've pick up the idea from Spring framework tests)


You can load the entity, close the session, and then assert that a LazyInitializationException is thrown when you access the property.


I have found an other solution :
org.hibernate.Hibernate.isPropertyInitialized(pojo, propertyName) check if the property is initialized..

The trick of the LazyInitializationException force to close the session wich will result of problem for the others test & to reopen one if this one was shared.


We didn't succeed in using isPropertyInitialized(entity, "property") but isInitialized(entity.getProperty()) works well

0

精彩评论

暂无评论...
验证码 换一张
取 消