The context
I have a simple test method testFindByUserName. I use mockito library. I have @Mock UserMapper which is created by Mapstruct library.
The problem
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);
Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot 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.
How resolve this problem.
The 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<>(),new ArrayList<>(), new HashSet<>());
    token = "test_token";
    userDto = new UserDto(2L, "User_test",
            "firstName_test", "lastName_test",
            "test@test.pl", true, "password_test",
            "address_test", null,new ArrayList<>(),new ArrayList<>(), new HashSet<>());
}
@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());
}
method which I testing
@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);
}
 
     
    