title: org.hibernate.LazyInitializationException: could not initialize proxy - no Session . For a Testcase JUnit
body: <p>I have two controllers which are given below, I've been trying to write test cases for both of them but since the controllers user @AuthenticationPrincipal. I am having a hard time writing the test case for them and been getting a error while running the test case .</p>
  
  <pre><code>@ResponseBody
  @RequestMapping(value = "/view/{id}", method = RequestMethod.GET, produces = "application/pdf")
  public ResponseEntity renderDocument(@AuthenticationPrincipal Principal principal, @PathVariable("id") Long id)
          throws IOException {
      Journal journal = journalRepository.findOne(id);
      Category category = journal.getCategory();
      CurrentUser activeUser = (CurrentUser) ((Authentication) principal).getPrincipal();
      User user = userRepository.findOne(activeUser.getUser().getId());
      List&lt;Subscription&gt; subscriptions = user.getSubscriptions();
      Optional&lt;Subscription&gt; subscription = subscriptions.stream()
              .filter(s -&gt; s.getCategory().getId().equals(category.getId())).findFirst();
      if (subscription.isPresent() || journal.getPublisher().getId().equals(user.getId())) {
          File file = new File(PublisherController.getFileName(journal.getPublisher().getId(), journal.getUuid()));
          InputStream in = new FileInputStream(file);
          return ResponseEntity.ok(IOUtils.toByteArray(in));
      } else {
          return ResponseEntity.notFound().build();
      }
  
  }
  
  @RequestMapping(method = RequestMethod.POST, value = "/publisher/publish")
  @PreAuthorize("hasRole('PUBLISHER')")
  public String handleFileUpload(@RequestParam("name") String name, @RequestParam("category")Long categoryId, @RequestParam("file") MultipartFile file,
          RedirectAttributes redirectAttributes, @AuthenticationPrincipal Principal principal) {
  
      CurrentUser activeUser = (CurrentUser) ((Authentication) principal).getPrincipal();
      Optional&lt;Publisher&gt; publisher = publisherRepository.findByUser(activeUser.getUser());
  
      String uuid = UUID.randomUUID().toString();
      File dir = new File(getDirectory(publisher.get().getId()));
      createDirectoryIfNotExist(dir);
  
      File f = new File(getFileName(publisher.get().getId(), uuid));
      if (!file.isEmpty()) {
          try {
              BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(f));
              FileCopyUtils.copy(file.getInputStream(), stream);
              stream.close();
              Journal journal = new Journal();
              journal.setUuid(uuid);
              journal.setName(name);
              journalService.publish(publisher.get(), journal, categoryId);
              return "redirect:/publisher/browse";
          } catch (Exception e) {
              redirectAttributes.addFlashAttribute("message",
                      "You failed to publish " + name + " =&gt; " + e.getMessage());
          }
      } else {
          redirectAttributes.addFlashAttribute("message",
                  "You failed to upload " + name + " because the file was empty");
      }
  
      return "redirect:/publisher/publish";
  }
  </code></pre>
  
  <p>The Test Case is </p>
  
  <pre><code>@Test
      @WithMockUser(username = "user1", roles = { "USER" })
      public void renderDocument() throws Exception {
  
  
          mockMvc.perform(get("/view/{id}", 1)).andExpect(content().contentType(contentType)).andExpect(status().isOk());
      }
  </code></pre>
  
  <p>But i am getting a following error :- </p>
  
  <pre><code>org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
      at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
      at com.crossover.trial.journals.model.Publisher_$$_jvst650_0.toString(Publisher_$$_jvst650_0.java)
      at org.springframework.web.util.UriComponents.getVariableValueAsString(UriComponents.java:269)
      at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:234)
      at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:685)
      at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:328)
      at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:47)
      at org.springframework.web.util.UriComponents.expand(UriComponents.java:163)
      at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:412)
      at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.&lt;init&gt;(MockHttpServletRequestBuilder.java:122)
      at org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get(MockMvcRequestBuilders.java:55)
      at com.crossover.trial.journals.rest.JournalServiceTest.renderDocument(JournalServiceTest.java:187)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  </code></pre>
  
  <p>JournalServiceTest.java</p>
  
  <pre><code>package com.crossover.trial.journals.rest;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertNotNull;
  import static org.junit.Assert.fail;
  import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
  
  import java.nio.charset.Charset;
  import java.util.List;
  import java.util.Optional;
  
  import com.crossover.trial.journals.Application;
  import com.crossover.trial.journals.model.Journal;
  import com.crossover.trial.journals.model.Publisher;
  import com.crossover.trial.journals.model.User;
  import com.crossover.trial.journals.repository.PublisherRepository;
  import com.crossover.trial.journals.service.JournalService;
  import com.crossover.trial.journals.service.ServiceException;
  import com.crossover.trial.journals.service.UserService;
  
  import org.junit.Before;
  import org.junit.FixMethodOrder;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.junit.runners.MethodSorters;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.SpringApplicationConfiguration;
  import org.springframework.http.MediaType;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.web.context.WebApplicationContext;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Application.class)
  @WebAppConfiguration
  @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  public class JournalServiceTest {
  
      private final static String NEW_JOURNAL_NAME = "New Journal";
  
      @Autowired
      private JournalService journalService;
  
      @Autowired
      private UserService userService;
  
      @Autowired
      private PublisherRepository publisherRepository;
  
      private MockMvc mockMvc;
  
      @Autowired
      private WebApplicationContext webApplicationContext;
  
      @Before
      public void setup() throws Exception {
          this.mockMvc = webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
      }
  
      @Test
      public void browseSubscribedUser() {
          List&lt;Journal&gt; journals = journalService.listAll(getUser("user1"));
          assertNotNull(journals);
          assertEquals(1, journals.size());
  
          assertEquals(new Long(1), journals.get(0).getId());
          assertEquals("Medicine", journals.get(0).getName());
          assertEquals(new Long(1), journals.get(0).getPublisher().getId());
          assertNotNull(journals.get(0).getPublishDate());
      }
  
      @Test
      public void browseUnSubscribedUser() {
          List&lt;Journal&gt; journals = journalService.listAll(getUser("user2"));
          assertEquals(0, journals.size());
      }
  
      @Test
      public void listPublisher() {
          User user = getUser("publisher1");
          Optional&lt;Publisher&gt; p = publisherRepository.findByUser(user);
          List&lt;Journal&gt; journals = journalService.publisherList(p.get());
          assertEquals(2, journals.size());
  
          assertEquals(new Long(1), journals.get(0).getId());
          assertEquals(new Long(2), journals.get(1).getId());
  
          assertEquals("Medicine", journals.get(0).getName());
          assertEquals("Test Journal", journals.get(1).getName());
          journals.stream().forEach(j -&gt; assertNotNull(j.getPublishDate()));
          journals.stream().forEach(j -&gt; assertEquals(new Long(1), j.getPublisher().getId()));
  
      }
  
      @Test(expected = ServiceException.class)
      public void publishFail() throws ServiceException {
          User user = getUser("publisher2");
          Optional&lt;Publisher&gt; p = publisherRepository.findByUser(user);
  
          Journal journal = new Journal();
          journal.setName("New Journal");
  
          journalService.publish(p.get(), journal, 1L);
      }
  
      @Test(expected = ServiceException.class)
      public void publishFail2() throws ServiceException {
          User user = getUser("publisher2");
          Optional&lt;Publisher&gt; p = publisherRepository.findByUser(user);
  
          Journal journal = new Journal();
          journal.setName("New Journal");
  
          journalService.publish(p.get(), journal, 150L);
      }
  
      @Test()
      public void publishSuccess() {
          User user = getUser("publisher2");
          Optional&lt;Publisher&gt; p = publisherRepository.findByUser(user);
  
          Journal journal = new Journal();
          journal.setName(NEW_JOURNAL_NAME);
          journal.setUuid("SOME_EXTERNAL_ID");
          try {
              journalService.publish(p.get(), journal, 3L);
          } catch (ServiceException e) {
              fail(e.getMessage());
          }
  
          List&lt;Journal&gt; journals = journalService.listAll(getUser("user1"));
          assertEquals(2, journals.size());
  
          journals = journalService.publisherList(p.get());
          assertEquals(2, journals.size());
          assertEquals(new Long(3), journals.get(0).getId());
          assertEquals(new Long(4), journals.get(1).getId());
          assertEquals("Health", journals.get(0).getName());
          assertEquals(NEW_JOURNAL_NAME, journals.get(1).getName());
          journals.stream().forEach(j -&gt; assertNotNull(j.getPublishDate()));
          journals.stream().forEach(j -&gt; assertEquals(new Long(2), j.getPublisher().getId()));
      }
  
      @Test(expected = ServiceException.class)
      public void unPublishFail() {
          User user = getUser("publisher1");
          Optional&lt;Publisher&gt; p = publisherRepository.findByUser(user);
          journalService.unPublish(p.get(), 4L);
      }
  
      @Test(expected = ServiceException.class)
      public void unPublishFail2() {
          User user = getUser("publisher1");
          Optional&lt;Publisher&gt; p = publisherRepository.findByUser(user);
          journalService.unPublish(p.get(), 100L);
      }
  
      @Test
      public void unPublishSuccess() {
          User user = getUser("publisher2");
          Optional&lt;Publisher&gt; p = publisherRepository.findByUser(user);
          journalService.unPublish(p.get(), 4L);
  
          List&lt;Journal&gt; journals = journalService.publisherList(p.get());
          assertEquals(1, journals.size());
          journals = journalService.listAll(getUser("user1"));
          assertEquals(1, journals.size());
      }
  
      @Test
      public void renderDocument() throws Exception {
          mockMvc.perform(get("/view/{id}", "1"));
      }
  
      protected User getUser(String name) {
          Optional&lt;User&gt; user = userService.getUserByLoginName(name);
          if (!user.isPresent()) {
              fail("user1 doesn't exist");
          }
          return user.get();
      }
  
  }
  </code></pre>