Mockito - Function that receives jsonObject return false

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:

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

What results:

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 when using the when.thenReturn, it would be as if I skipped the execution of the method in question. It would only bring the return. What can I do?

    
asked by anonymous 23.10.2017 / 15:54

1 answer

2

Your code has fallen into a corner case of Mockito in relation to stubbing > in spies .

Link to relevant documentation

Free translation

  

Prank important when spying on real objects!

     

Sometimes it is impossible or it is impractical to use when(Object) to make stubbing of spies. That way, when using spies please consider the doReturn|Answer|Throw() to stubbing methods family. 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

    
23.10.2017 / 17:59