title: Spring Boot: Use Hibernate Session in a unit test
body: <p>In my Spring Boot application I access my Hibernate session as shown in this answer: <a href="https://stackoverflow.com/a/33881946/272180">https://stackoverflow.com/a/33881946/272180</a></p>
  
  <p>Now I also want to access the Hibernate Session in a unit test.</p>
  
  <p>How can I set up the datasource and access the Hibernate Session in a unit test of a Spring Boot application?</p>
  
  <p>When I simply Autowire it, I get <code>org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread.</code></p>
  
  <p>Autowiring and using a <code>@Service</code> works flawlessly.</p>
  
  <p>My unit-testing class looks like this atm:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = App.class)
  @WebIntegrationTest
  public class WebTest {
  
      @Autowired
      private SessionFactory  sessionFactory;
  
      @Autowired
      private UserService     userService;
  
      @Test
      public void testService() {
  
          final List&lt;User&gt; users = userService.getUsers();
  
          // do something with users
          // ...
  
      }
  }
  </code></pre>
  
  <p><code>App.class</code> refers to the class with the main method which is used to run the Spring Boot application.</p>