Mockito - Function that receives jsonObject return false

Asked

Viewed 181 times

1

public class ParseProcessoTest {
    private final String PATTERN_DATA_HORA = "yyyy-MM-dd HH:mm:ss";
    JSONObject jsonObject = new JSONObject("json qualquer");
    JSONObject jsonMov = new JSONObject("json qualquer 2");
    ParseProcesso parseProcesso = Mockito.spy(new ParseProcesso(jsonObject));
    JSONObject spyJson = Mockito.spy(jsonMov);


    @Before
    public void init(){
        MockitoAnnotations.initMocks(this);
        when(parseProcesso.movimentacaoTemAnexo(spyJson)).thenReturn(false);
    }

    @Test
    public void testaSeRetornaFalsoMetodoMovimentacaoTemAnexo(){
        Assertions.assertThat(parseProcesso.movimentacaoTemAnexo(spyJson)).isFalse();
    }

    @Test
    public void testaParse() throws IOException {
        Processo processoTeste = parseProcesso.parse();
        //testes

I have this test class, which tests the parse of a set of information. However, I want the following method to return false:

public boolean movimentacaoTemAnexo(JSONObject movimento){
    return (int) movimento.get("qtd_anexo") > 0;
}

I’ve tried to:

when(parseProcesso.movimentacaoTemAnexo(anyObject())).thenReturn(false);

Resulting:

java.lang.NullPointerException
at br.com.ParseProcesso.movimentacaoTemAnexo(ParseProcesso.java:208)
at testes.ParseProcessoTest.init(ParseProcessoTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I thought that using when.thenReturn would be like skipping the execution of the method in question. It would only bring the return. What can I do?

  • It launched a Nullpointerexception... I changed the Rangefield() method using Optional.ofNullable to avoid this Exception, but still...

  • I’ve updated the question! And the parts are respectively: the Return of the moving methodTemAnexo() and the when(parseProcesses.movesTemAnexo(anyObject())). thenReturn(false);

  • No, there is only this method with these parameters. I had tried, I tried now but the problem remains the same.

1 answer

2


Your code fell into a corner case of Mockite in relation to stubbing in Spies.

Link to relevant documentation

Free translation

Prank important when spying on real objects!

Sometimes it is impossible or not practical to use when(Object) to do the stubbing spies. That way, when using spies please consider the family of methods doReturn|Answer|Throw() for stubbing. Example:

List list = new LinkedList();
List spy = spy(list);

// Impossivel: o método real é chamado, então spy.get(0) dispara 
// IndexOutOfBoundsException (a lista ainda está vazia)
when(spy.get(0)).thenReturn("foo");
// Você deve usar doReturn() para stubbing
doReturn("foo").when(spy).get(0);

In your case, just change the line

when(parseProcesso.movimentacaoTemAnexo(spyJson)).thenReturn(false);

To:

doReturn(false).when(parseProcesso).movimentacaoTemAnexo(any());

Source: Soen Mockito: Trying to Spy on method is Calling the original method

Browser other questions tagged

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