1

I want a method with varargs arguments but it isn't working because the method stills be called. I've already tryed the follow without success:

ArgumentMatchers.any()

new String[] {}

new String[0]

But in all these cases, the method still be called.

There is my classes:

@Service
public class ProjetoServiceImpl extends DefaultServiceImpl<Projeto,ProjetoRepository> implements ProjetoService { 

  @Override
  public List<Projeto> buscaAtestadosComFiltro(String[] filtros) {
    List<Projeto> projetos = buscarTodos(); // I want to mock this line
    projetos = filtraResultado(projetos, filtros);
    return projetos;
  }
}

////////////////////////////////////////////////////

public class ProjetoServiceImplTest { 
    private ProjetoServiceImpl projImpl;
    @MockBean
    private ProjetoService projetoServiceMock;

    @Test
    public void testaBuscaAtestadosComFiltro() {
       Projeto projeto1 = new Projeto();
       List<Projeto> projetos = new ArrayList<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);
   }
}
oitathi
  • 153
  • 2
  • 3
  • 17
  • 1
    Check my answer [`here`](https://stackoverflow.com/questions/57496753/how-to-mock-resttemplate-getforobject). You'll probably need to match the parameters to an empty `Object[]`, if your implementation does not pass a parameter. – second Aug 15 '19 at 16:33
  • `String...` equivale à `String[]`.... I mean, `String...` is equivalent to `String[]`, you may try passing an empty array or even `null`, like `...projectServiceMock.buscarTodos(new String[0])).then...` – user85421 Aug 15 '19 at 16:34
  • Possible duplicate of [How to properly match varargs in Mockito](https://stackoverflow.com/questions/2631596/how-to-properly-match-varargs-in-mockito) – Tobias Aug 15 '19 at 17:36
  • I've already tryed all the suggestions, incluiding the How to properly match varargs in Mockito link, and I did not get success. Any other suggestion? – oitathi Aug 15 '19 at 19:01
  • Please provide a [mre]. Where is the real method called and what parameters are actually used there? Also if you are saying its not working what exactly are you are refering to? – second Aug 15 '19 at 19:22
  • If you think that you followed the instructions that have already been provided, then provide that code. A varargs String is shorthand for an array of Strings, which has been mentioned already. So if you really tried that, it would have worked. That means that something you typed was wrong when you tried it. Provide that code so we can help. – searchengine27 Aug 15 '19 at 19:53

1 Answers1

1

The problem is that the mock you are creating has nothing to do with projImpl.

Also @MockBean is a spring annotation, not a mockito annotation and does nothing for your usecase.

You could use a spy on ProjetoServiceImpl instead, however some people would suggest that you should refactor your code. Moving the buscarTodos method to some dependency and mocking that is a viable option..

The test would look like that when you use a spy:

@Test
public void testaBuscaAtestadosComFiltro() {
    Projeto projeto1 = new Projeto();
    List<Projeto> projetos = new ArrayList<Projeto>();
    projetos.add(projeto1);

    ProjetoServiceImpl projImpl = new ProjetoServiceImpl();
    ProjetoServiceImpl spy = Mockito.spy(projImpl);

    Mockito.when(spy.buscarTodos(ArgumentMatchers.<String>any())).thenReturn(projetos); 

    String[] filtros = new String[] {"java"};
    List<Projeto> result = spy.buscaAtestadosComFiltro(filtros);
    Assert.assertNotNull(result);
}

Another alternative (and cleaner) solution might be that you mock the defaultRepository in the implementation of buscarTodos instead, which could be seen in code example you previously had in your question.

In your test case you would then need to provide the matching result (projetos) when findAll() is called.

second
  • 4,069
  • 2
  • 9
  • 24