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...
– laaf
I’ve updated the question! And the parts are respectively: the Return of the moving methodTemAnexo() and the when(parseProcesses.movesTemAnexo(anyObject())). thenReturn(false);
– laaf
No, there is only this method with these parameters. I had tried, I tried now but the problem remains the same.
– laaf
Let’s go continue this discussion in chat.
– Anthony Accioly