java – Error accessing dao class by test class

Question:

I have the following error when accessing a dao class from 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 test class method:

@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 am using Dependency Injection. The error occurs when accessing the DAO method, I checked the project to see if it was the injection, and I didn't find 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

Answer:

The error occurs because its variable launchDao is null.

Your dependency injection engine (as I understand it is the peak) is not running in the test, so it doesn't inject your controller's dependencies into it.

For a simpler solution, you can create a constructor that takes the controller's dependency, and in your test use a Mock to simulate what your dependency should do.

I recommend using the Mockito framework to facilitate mocking.

Your controller:

public class EditarLancamentoController(LancamentoDao lancamentoDao) {
    this.lancamentoDao = lancamentoDao;
}

In your test:

@Test
public void testConsultarLancamento() {
    LancamentoDao lancamentoDao = Mockito.mock(LancamentoDao.class);
    EditarLancamentoController instanciaEditarLancamentoController = new EditarLancamentoController(lancamentoDao);
    int resultadoDaConsultaUm = instanciaEditarLancamentoController.consultarLancamento(4);      
    int resultadoDaConsultaNulo = instanciaEditarLancamentoController.consultarLancamento(0);      
    assertEquals(4, resultadoDaConsultaUm);
    assertEquals(0, resultadoDaConsultaNulo);
}
Scroll to Top