Error accessing dao class by test class

Asked

Viewed 201 times

3

I have the following error, when accessing a dao class 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 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 at the time of accessing the DAO method, I checked the design to see if it was the injection, and found no 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
  • Adds the full error stack.

  • @diegofm, added the description

  • Where do you use codLankmentReceived within query Shouldn’t be codLanking? I think the problem is this...

  • Correct variable, but nothing has changed.

  • Are you using spring for dependency injection? Do you use JPA in your daos? If you use jpa, the connection in the database is in the container or persistence.xml?

  • @Giulianabezerra I’m using the container peak and use JPA in the daos. my connection is in persistence

  • Actually, it’s the variable lancamentoDao is null? You initialized this variable before calling the method consultarLancamento by testing?

Show 2 more comments

1 answer

0

The error occurs because its variable lancamentoDao is null.

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

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

I recommend using the framework Mockite to facilitate in mocks.

Your controller:

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

On 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.