title: Unit testing methods using Principal from Spring Secucrity
body: <p>Could someone tell me how should I test those two methods:</p>
  
  <pre><code>public boolean deleteUser(Principal principal) {
      if (findLoggedInUser(principal) != null) {
          userRepository.delete(findLoggedInUser(principal));
          return true;
      }
      return false;
  }
  
  public User findLoggedInUser(Principal principal) {
      return findUserbyUsername(principal.getName());
  }
  </code></pre>
  
  <p>The problem is that I'm using currently logged in user with basic authentication and don't know how, and if I can mock those Principals. Is there a way to do that? Those methods are in my Service layer so maybe I can't do unit tests and I'm left with integration tests because those methods heavily use DB?</p>
  
  <p>EDIT 1:
  <strong>My changed test class:</strong></p>
  
  <pre><code>public class UserServiceBeanTest {
  
  @Spy
  @InjectMocks
  private UserServiceBean userServiceBean;
  
  @Mock
  private UserRepository userRepository;
  
  @Mock
  private Principal principal;
  
  @Mock
  private PasswordEncoder passwordEncoder;
  
  @Mock
  private User userStub;
  
  private String defaultName = "user";
  private  String defaultPassword = "password";
  private String defaultEmail = "example@example.com";
  
  @Before
  public void init() {
      MockitoAnnotations.initMocks(this);
  }
  
  
  @Test
  public void shouldReturnTrue_whenUserDeleted() {
      //given
      when(principal.getName()).thenReturn(defaultName);
      when(userServiceBean.findLoggedInUser(principal)).thenReturn(userStub);
  
      // when
      boolean removed = userServiceBean.deleteUser(principal);
  
      //then
      assertTrue(removed);
      verify(userRepository, times(1));
  }
  
  @Test
  public void shouldReturnFalse_whenUserNotFound() {
      //given
      when(principal.getName()).thenReturn(defaultName);
      when(userServiceBean.findLoggedInUser(principal)).thenReturn(null);
  
      //when
      boolean removed = userServiceBean.deleteUser(principal);
  
      //then
      assertFalse(removed);
      verify(userRepository, times(0));
  }
  
  }
  </code></pre>
  
  <p>I'm getting those errors now:</p>
  
  <pre><code>org.mockito.exceptions.misusing.UnfinishedVerificationException: 
  </code></pre>
  
  <p>Missin</p>
  
  <pre><code>g method call for verify(mock) here:
  -&gt; at com.doublemc.services.UserServiceBeanTest.shouldReturnTrue_whenUserDeleted(UserServiceBeanTest.java:63)
  
  Example of correct verification:
      verify(mock).doSomething()
  
  Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
  Those methods *cannot* be stubbed/verified.
  Mocking methods declared on non-public parent classes is not supported.
  
  
      at com.doublemc.services.UserServiceBeanTest.init(UserServiceBeanTest.java:48)
  </code></pre>
  
  <p>EDIT 2:
  <strong>Here is my UserServiceBean class:</strong></p>
  
  <pre><code>package com.doublemc.services;
  
  import com.doublemc.domain.ToDoItem;
  import com.doublemc.domain.User;
  import com.doublemc.repositories.UserRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.stereotype.Service;
  
  import javax.transaction.Transactional;
  import java.security.Principal;
  
  @Service
  @Transactional
  public class UserServiceBean {
  
  private final UserRepository userRepository;
  private final PasswordEncoder passwordEncoder;
  
  @Autowired
  UserServiceBean(UserRepository userRepository, PasswordEncoder passwordEncoder) {
      this.userRepository = userRepository;
      this.passwordEncoder = passwordEncoder;
  }
  
  public User saveUser(User user) {
      User newUser = new User();
      newUser.setUsername(user.getUsername());
      newUser.setEmail(user.getEmail());
      newUser.setPassword(passwordEncoder.encode(user.getPassword()));
      return userRepository.save(newUser);
  }
  
  public boolean userExists(User user) {
      return userRepository.findByUsername(user.getUsername()) != null;
  }
  
  public Iterable&lt;ToDoItem&gt; getAllToDoItems(User user) {
      return user.getToDoItems();
  }
  
  public boolean deleteUser(Principal principal) {
      if (findLoggedInUser(principal) != null) {
          userRepository.delete(findLoggedInUser(principal));
          return true;
      }
      return false;
  }
  
  public User findLoggedInUser(Principal principal) {
      return userRepository.findByUsername(principal.getName());
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Here is my UserRepository:</strong></p>
  
  <pre><code>public interface UserRepository extends CrudRepository&lt;User, Long&gt; {
  User findByUsername(String username);
  }
  </code></pre>
  
  <p>EDIT 6: I created myself three more tests:</p>
  
  <pre><code>@Test
  public void shouldReturnUser_whenPassedUser() {
      // given
      when(userRepository.save(any(User.class))).thenReturn(new User(defaultName, defaultPassword, defaultEmail));
  
      // when
      User savedUser = userServiceBean.saveUser(userStub);
  
      // then
      assertNotNull(savedUser);
      verify(userRepository, times(1)).save(any(User.class));
  }
  
  @Test
  public void shouldReturnTrue_whenUserExists() {
      // given
      when(userStub.getUsername()).thenReturn(defaultName);
      when(userRepository.findByUsername(userStub.getUsername())).thenReturn(userStub);
  
      // when
      boolean exists = userServiceBean.userExists(userStub);
  
      // then
      assertTrue(exists);
      verify(userRepository, times(1)).findByUsername(defaultName);
  }
  
  @Test
  public void shouldReturnFalse_whenUserNotFoundByUsername() {
      // given
      when(userStub.getUsername()).thenReturn(defaultName);
      when(userRepository.findByUsername(userStub.getUsername())).thenReturn(null);
  
      // when
      boolean exists = userServiceBean.userExists(userStub);
  
      // then
      assertFalse(exists);
      verify(userRepository, times(1)).findByUsername(defaultName);
  }
  </code></pre>
  
  <p>And here are tested methods:
  <strong>UserServiceBean.saveUser</strong>:</p>
  
  <pre><code>public User saveUser(User user) {
      User newUser = new User(user.getUsername(), user.getEmail(), passwordEncoder.encode(user.getPassword()));
      return userRepository.save(newUser);
  }
  </code></pre>
  
  <p><strong>UserServiceBean.userExists</strong>:</p>
  
  <pre><code>public boolean userExists(User user) {
      return userRepository.findByUsername(user.getUsername()) != null;
  }
  </code></pre>