OutofMemoryError while running Junit Tests for JPA
When running unit tests in a Spring-JPA application, I ran in to out of memory issue. The exact error was:
2008-05-01 14:43:32,944 DEBUG org.springframework.test.context.junit4.SpringMethodRoadie –
Test method public void
…repository.ApplicationHibernateRepositoryTest.testFindApplication() threw exception:
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
Exception in thread “Thread-2” java.lang.OutOfMemoryError: Java heap space
The cause was that I had an entity with too many relationships to other entities.
A slight overlook fron my side caused all these relationships to be eagerly loaded (added to the fact that this is the default behavior for some associations in JPA).
I just went back and declared the relationships to be lazily loaded (using fetch=FetchType.LAZY) and the error just disappeared.
I believe it is possible to increase heap size for running Junit tests but in this case, I really didn’t need all the entities loaded eagerly.