title: Mockito and Java Spring - repository in tests
body: <p>I am new to mockito / Java Spring and I tried to make a test. I have an admin controller, with this method in it :</p>
  
  <pre><code>@RequestMapping(value="/admin/users", method = RequestMethod.GET)
  public ResponseEntity&lt;List&lt;User&gt;&gt;users(){
      List&lt;User&gt; students=this.userService.getAll();
      if(students.isEmpty())
          return new ResponseEntity&lt;List&lt;User&gt;&gt;(HttpStatus.NO_CONTENT);
      return  new ResponseEntity&lt;List&lt;User&gt;&gt;(students,HttpStatus.OK);
  }
  </code></pre>
  
  <p>Now, I tried to make a test, to see if it works, something like this :</p>
  
  <pre><code>public class AdminControllerTest {
  
      @InjectMocks
      private AdminController controller;
  
      @InjectMocks
      private UserServiceImpl userService = new UserServiceImpl();
  
      @Mock
      private UserRepository userRepository;
  
      private MockMvc mockMvc;
  
      @Before
      public void setup(){
  
          MockitoAnnotations.initMocks(this);
  
          mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
      }
  
      @Test
      public void test() throws Exception {
  
          User user = new User();
          user.setId(5);
          user.setActive(true);
          user.setLastName("Test1");
          user.setName("Test1");
          user.setPassword("123123");
          user.setRole(User.Role.student);
          user.setEmail("test@gmail.com");
          when(userService.save(user)).thenReturn(user);
          userService.save(user);
          mockMvc.perform(get("/admin/users")).andDo(print());
  
      }
  
  }
  </code></pre>
  
  <p>The problem is that I am not sure how to make the Test class add items to the repository. I tried it this way but I get NullPointerExceptions. I am guessing it is because the repository is empty and when the .getAll() method is called it returns the error.</p>