1
I would like to mock this method:
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<E> buscarTodos(String ... sorting) {
Sort sort = null;
if (sorting != null && sorting.length > 0) {
sort = new Sort(Sort.Direction.ASC, sorting);
return defaultRepository.findAll(sort);
}
return defaultRepository.findAll();
}
I’ve tried so many ways:
@Test
public void testaBuscaAtestadosComFiltro() {
List<Projeto> projetos = new ArrayList<Projeto>();
projetos.add(projeto1);
when(projetoServiceMock.buscarTodos()).thenReturn(projetos);
//when(projetoServiceMock.buscarTodos(ArgumentMatchers.<String>any())).thenReturn(projetos);
//when(projetoServiceMock.buscarTodos(new String[] {})).thenReturn(projetos);
//when(projetoServiceMock.buscarTodos(new String[0])).thenReturn(projetos);
}
and none worked. does anyone have any idea what I’m doing wrong?
Follow my test class:
public class ProjetoServiceImplTest {
private ProjetoServiceImpl projImpl;
@MockBean
private ProjetoService projetoServiceMock;
@Test
public void testaBuscaAtestadosComFiltro() {
List<Projeto> projetos = new ArrayList<Projeto>();
Projeto projeto1 = new Projeto()
projetos.add(projeto1);
when(projetoServiceMock.buscarTodos(ArgumentMatchers.<String>any())).thenReturn(projetos);
String[] filtros = new String[] {"java"};
projImpl = new ProjetoServiceImpl();
List<Projeto> result = projImpl.buscaAtestadosComFiltro(filtros);
assertNotNull(result);
}
And that’s the class to be tested:
public class ProjetoServiceImpl{
@Override
public List<Projeto> buscaAtestadosComFiltro(String[] filtros) {
List<Projeto> projetos = buscarTodos(); // eu quero mockar essa linha
projetos = filtraResultado(projetos, filtros);
return projetos ;
}
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<E> buscarTodos(String ... sorting) {
//não quero q esse metodo seja chamado!
}
you put the class you are trying to mock, the ideal is to understand the class that is calling her, after all what the stoned class does not matter much, I believe, now looking of ready bate I would use the Mockito.any()
– Scarabelo
Which version of Mockito is using?
– nullptr
I put more details of the Class
– oitathi
And which version of Mockito is using?
– nullptr
Now with the full code I saw q ta Zica this right there, I had never gone through this situation, I found even a related Issue in mockite git. https://github.com/mockito/mockito/issues/1222
– Scarabelo
using version 2.28.2, but already tried with the last too
– oitathi