title: How to use the mapper created by Mapstruct as @Mock during testing
body: <p><strong>The context</strong></p>
  
  <p>I have a simple test method testFindByUserName. I use mockito library. I have @Mock UserMapper which is created by Mapstruct library.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>Mocito doesn't handle Static method INSTANCE which I use to mapping user to userDto. I have error : error:org.mockito.exceptions.misusing.MissingMethodInvocationException: 
  when() requires an argument which has to be 'a method call on a mock'.
  For example:
      when(mock.getArticles()).thenReturn(articles);</p>
  
  <p>Also, this error might show up because:
  1. you stub either of: final/private/equals()/hashCode() methods.
     Those methods <em>cannot</em> be stubbed/verified.
     Mocking methods declared on non-public parent classes is not supported.
  2. inside when() you don't call method on mock but on some other object. </p>
  
  <p>How resolve this problem. </p>
  
  <p><strong>The code</strong></p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  @SpringBootTest
  public class UserServiceImplTest {
  
  private User user;
  
  private String token;
  
  private UserDto userDto;
  
  
  @InjectMocks
  private UserServiceImpl userService;
  
  @Mock
  private UserMapper userMapper;
  
  @Before
  public void before() {
      user = new User(2L, "User_test",
              "firstName_test", "lastName_test",
              "test@test.pl", true, "password_test",
              "address_test", null,new ArrayList&lt;&gt;(),new ArrayList&lt;&gt;(), new HashSet&lt;&gt;());
      token = "test_token";
      userDto = new UserDto(2L, "User_test",
              "firstName_test", "lastName_test",
              "test@test.pl", true, "password_test",
              "address_test", null,new ArrayList&lt;&gt;(),new ArrayList&lt;&gt;(), new HashSet&lt;&gt;());
  
  }
  
  @Test
  public void testFindByUsername() throws Exception {
      //Given
      String username= "User_test";
  
      when(userMapper.INSTANCE.userToUserDto(userRepository.findByUsername(username))).thenReturn(userDto);
      //When
      UserDto result = userService.findByUsername(username);
  
      //Then
      assertEquals("User_test", result.getUsername());
      assertEquals("firstName_test", result.getFirstName());
      assertEquals("lastName_test", result.getLastName());
      assertEquals("test@test.pl", result.getEmail());
      assertEquals("password_test", result.getPassword());
      assertEquals("address_test", result.getAddress());
  
  }
  </code></pre>
  
  <p>method which I testing</p>
  
  <pre><code>@Override
  public UserDto findByUsername(final String username)  {
      User user = userRepository.findByUsername(username);
      if (user == null) {
          LOGGER.info("Not found user");
      }
          return mapper.INSTANCE.userToUserDto(user);
  }
  </code></pre>