I have the following error, when accessing a class dao of a test class:
java.lang.NullPointerException
This is the controller method:
@Post("/consultar_lancamento/{codLancamento}")
public int consultarLancamento(int codLancamento) {
try {
Lancamento lancamento = lancamentoDao.carregaPorId(codLancamento);
result.use(json()).withoutRoot().from(lancamento).serialize();
contaLinhasDoLancamento(lancamento);
} catch (Exception err) {
Lancamento lancamento = new Lancamento();
lancamento.setDescricaoLancamento(null);
result.use(json()).withoutRoot().from(lancamento).serialize();
return 0;
}
return codLancamento;
}
This is the method of the test class:
@Test
public void testConsultarLancamento() {
EditarLancamentoController instanciaEditarLancamentoController = new EditarLancamentoController();
int resultadoDaConsultaUm = instanciaEditarLancamentoController.consultarLancamento(4);
int resultadoDaConsultaNulo = instanciaEditarLancamentoController.consultarLancamento(0);
assertEquals(4, resultadoDaConsultaUm);
assertEquals(0, resultadoDaConsultaNulo);
}
Note: I'm using dependency injection. The error occurs when accessing the DAO method, I checked the project to see if it was the injection, and I did not encounter any configuration problems.
Error description:
Testsuite: br.com.uprise.controller.EditarLancamentoControllerTest
Tests run: 3, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1,125 sec
Testcase: testConsultarLancamento(br.com.uprise.controller.EditarLancamentoControllerTest): Caused an ERROR
null
java.lang.NullPointerException
at br.com.uprise.controller.EditarLancamentoController.consultarLancamento(EditarLancamentoController.java:106)
at br.com.uprise.controller.EditarLancamentoControllerTest.testConsultarLancamento(EditarLancamentoControllerTest.java:52)
Test br.com.uprise.controller.EditarLancamentoControllerTest FAILED