0

I am getting org.hibernate.LazyInitializationException from my TestNG TestClases.

I have extended my TestNG test class with AbstractTransactionalTestNGSpringContextTests. One of my Test Object has a Lazy collection, which leads to this exception.

Is there any way to make all the collection initialized eagerly during Testing only. This exception does not occur if i do normal execution (No Test).

1 Answer 1

1

If you invoke your DAO/Services/Controllers directly (without MockMVC or RestAssured), then just mark the test as @Transactional. That way the transactions and the Hibernate Session is going to be opened before the test as well as closed after the test.

Otherwise it's opened by your service and is closed by the service.

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

2 Comments

I have used @Transactional, but the issue is with the object initialized in another Test Class. When i try to access the lazy Collection from another Test Class.
That shouldn't happen. Objects needed for test1 must be created in test1. And in general the order of the tests must not change the results.

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.