title: Spring Unit test JPA repository
body: <p>I'm new to Spring framework. I need to write Unit Test for JPA repository. I'm trying simple repository <code>saveAndFlush()</code> method. But nothing saves in my repository. Here is my source code:</p>
  
  <p>TestContext.class</p>
  
  <pre><code>@Configuration 
  @PropertySource("classpath:log4j.properties") 
  public class TestContext {
  
      @Bean
      public RoleService roleService() {
          return Mockito.mock(RoleService.class);
      }
  
      @Bean
      public RightService RightService() {
          return Mockito.mock(RightService.class);
      }
  
      @Bean
      public RoleRepository RoleRepository() {
          return Mockito.mock(RoleRepository.class); 
      }
  }
  </code></pre>
  
  <p>RoleServiceTest.class</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {TestContext.class})
  @WebAppConfiguration
  public class RoleServiceTest {
  
      @Autowired
      private RoleRepository roleRepository;
  
      @Test
      public void TestServices() throws Exception {
          RoleDetails first = new RoleDetails();
          first.setId("1");
          first.setDescription("First Description");
          first.setName("First");
          roleRepository.saveAndFlush(new RoleEntity(first));
          roleRepository.save(new RoleEntity(first));
          List&lt;RoleEntity&gt; roles = new ArrayList&lt;RoleEntity&gt;();
          roles = roleRepository.findAll();
          System.out.println(roles);
          assertEquals(1, roles.size());
      }
  }
  </code></pre>
  
  <p>And error:</p>
  
  <pre><code>java.lang.AssertionError: expected:&lt;1&gt; but was:&lt;0&gt;
  </code></pre>
  
  <p>I'm almost sure that problem occurs because of testContext.Class. I used this class to test my controller  and it worked well, but now i need to test my database and i don't know how to modify contextConfiguration. I hope somone will help me. Thanks in advance!</p>