Announcement Announcement Module
No announcement yet.
How do you test for lazyInitialization exception with spring transactions? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do you test for lazyInitialization exception with spring transactions?

    What is the best way to test for LazyInitializationException when using spring's transactional test framework?

    If I use @NotTransactional, then Hibernate can't find the session because it wasn't created, but if it I make it @Transactional, then the session is still active... so that means it's impossible to test for LazyInitializationException.

    What is the workaround that is clean and elegant?

  • #2
    Here's another problem with spring transactions and hibernate sessions. It's driving me nuts:

    This test method works 100%:
    	public void testDelete() {
    		deleteAllowedModuleController.delete( 1, 1 );
    		EducationFacility educationFacility =
    			jobPrepService.findEducationFacility( 1, true, false );
    		assertEquals( 0, educationFacility.getAllowedModules().size() );
    		assertNotNull( jobPrepService.findModule( 1, false ) );
    Here's the controller code that it's testing:
    	@RequestMapping( method = RequestMethod.GET )
    	public String delete( @RequestParam( "educationFacilityId" ) long educationFacilityId,
    	                      @RequestParam( "moduleId" ) long moduleId )
    		EducationFacility educationFacility =
    			jobPrepService.findEducationFacility( educationFacilityId, true, false );
    		educationFacility.removeAllowedModule( jobPrepService.findModule( moduleId, false ) );
    		return "redirect:/admin/viewEducationFacility.htm?educationFacilityId=" + 
    However, once deployed and I execute the code, it does nothing at all. The request parameters are being passed in correctly - everything is fine. The only problem is that since it's not inside spring's "transaction", or whatever is different about spring's test framework, it's not working at all now. The delete does not actually happen.

    There's a couple other methods like this too. How can I depend on the tests if they don't behave like the application?

    The same goes for lazy loads... I want my tests to work exactly like it will once I deploy it (
    Last edited by mystic; May 26th, 2009, 10:57 PM.


    • #3
      Oh, and by the way, inside the controller, the following was not present:

      jobPrepService.storeEducationFacility( educationFacility );
      Which meant that the object wasn't being persisted. Now, this is a really silly mistake on my part... HOWEVER, because of the way the transactional test work, spring/hibernate persisted my changes, which made the test pass. This is very, very bad behaviour since the web application will not have a session like this. I really want this test to fail if the storeXYZ method isn't even in the controller....



      • #4
        Here's another example where the test passes, yet it does not work:

        	@RequestMapping( method = RequestMethod.GET )
        	public String delete( @RequestParam( "categoryId" ) long categoryId ) {
        		Category category = jobPrepService.findCategory( categoryId );
        		jobPrepService.deleteCategory( categoryId );
        		return "redirect:/admin/viewQuestion.htm?questionId=" +
        This is pretty simple code... it just takes the id and deletes the object associated. The test code works fine. However, once deployed, Hibernate throws an error saying it will resave the object on cascade. I understand how to fix this error... but the fact that it's not throwing this error in my test is VERY disturbing.

        All of these problems are related to the same cause. Please help.