Java - When(). Thenreturn() Mockite Function

Asked

Viewed 124 times

1

@Mock
RequestUtils requestUtils;
    @Before
    public void init() throws IOException, ClassNotFoundException {
        MockitoAnnotations.initMocks(this);
        HtmlPage paginaPesquisa = requestUtils.executarPOST(anyObject(),anyObject());
        when(paginaPesquisa).thenReturn(this.getPaginaEstatica(URL_PESQUISA, PAGINA_RESULTADOS_PESQUISA_NOME, "ISO-8859-1" ));
        when(requestUtils.getPaginaResultados(anyObject())).thenReturn(this.getPaginaEstatica(URL_PESQUISA,PAGINA_RESULTADOS_PESQUISA_IDADE, "ISO-8859-1"));
        when(paginaPesquisa.getWebClient().getPage(anyObject().toString())).thenReturn(this.getPaginaEstatica(URL_PAGINA_INICIAL,PAGINA_INICIAL,"ISO-8859-1"));
    }

The problem is that in this last when:

when(paginaPesquisa.getWebClient().getPage(anyObject().toString()))

Gives a NullPointerException because there is no way to recover the webClient.

I searched Mockito’s functions for this case but found nothing... What to do?

  • I think that you need to put a thenReturn(paginaPesquisa.getWebClient()) before that when.

  • What do you mean? I tried and it didn’t work.

  • I misspelled. That was the idea: when(paginaPesquisa.getWebClient()).thenReturn(valorMockado)

  • I’ve tried, but the paginaPesquisa already this stoned before, so gives a Nullpointerexception no when()...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.