I have the following service:
package inventarios.service;
import inventarios.repository.LoginUsersRepository;
import inventarios.to.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LoginUsersService {
@Autowired
LoginUsersRepository usersRepository;
public List<LoginUser> findAll(){
return usersRepository.findAll();
}
}
And the following unit test:
import inventarios.service.LoginUsersService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
class GUITest {
@Mock
private LoginUsersService usersService;
@InjectMocks
private LoginWindow loginWindow = new LoginWindow();
@Test
void main() {
Mockito.when(usersService.findAll()).thenReturn(Collections.emptyList());
}
}
However when I run it I get:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class inventarios.service.LoginUsersService.
Mockito can only mock non-private & non-final classes. If you're not sure why you're getting this error, please report to the mailing list.
My class is public and it is not final. What could be wrong?
I found the problem, as this answer in English mentions, mockito needs several libraries, not only the core, so I replaced it
mockito-core
withmockito-all
the latest stable version and it worked great.