eu gostaria de mockar esse método:
@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();
}
Eu já tentei de diversas formas:
@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);
}
e nenhuma funcionou. alguém tem ideia do que estou fazendo errado?
Segue minha classe de teste:
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);
}
E essa é a classe a ser testada:
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!
}