Error accessing dao class by a test class

3

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
    
asked by anonymous 05.07.2016 / 19:36

1 answer

0

The error occurs because your lancamentoDao variable is null.

Your dependency injection engine (which I understand is the peak) is not running in the test, so it does not inject the dependencies of your controller into it.

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

I recommend using the framework Mockito to make it easier on the mocks.

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);
}
    
26.09.2016 / 23:12