title: How to mock private method in public method in Spring Boot with JUnit
body: <p>I'd like you ask a few questions and ask you for advice:</p>
  
  <p>I want to test my public method (I use Spring Boot, Mockito, JUnit):</p>
  
  <pre><code>@Service
  public class MyClass{
  
  public Long getClientId(List&lt;String&gt; nameSurname) throws AuthorizationException {
          Long operatorId;
          if(...){
          (... something not interesting ...)
              User user = getUserByLogin("AnthonyGates2");
              operatorId = nonNull(user) ? user.getOperatorId() : null;
          } else {
              List&lt;User&gt; users = getUserListByLogin("AnthinyGates");
              operatorId = isNotEmpty(users) ? return operatorId;
           return operatorId;
      }
  </code></pre>
  
  <p>How to test the method <code>getClientId</code>?</p>
  
  <p>Methods <code>getUserByLogin</code> and <code>getUserListByLogin</code> are private in this class (<code>MyClass</code>) but I have to mock the results of these private methods because these methods retrieve data from an external service.
  These private methods looks like:</p>
  
  <pre><code>User user = DelegateImpl.getDelegate().getUserByLogin(nameAndSurname);
  </code></pre>
  
  <p>DelegateImpl.getDelegate().getUserByLogin get data from database and that data have to be mocked like:</p>
  
  <pre><code>when(DelegateImpl.getDelegate().getUserByLogin(any())).thenReturn(user);
  </code></pre>
  
  <p>How can I test my public class? Should I use PowerMock/PowerMockito? Making these methods public is in my opinion ugly because these methods are called only in <code>MyClass</code>. I can't find a good tutorial in Internet for my case (Spring Boot, Mockito, JUnit). </p>
  
  <p>Thank you very much for all your tips!</p>
  
  <p>Best regards
  Matthew</p>