How to return the value according to the received parameter?

2

I needed to do mocking with DAO, but the method receives a worksheet object and according to the id attribute of the Worksheet needs to return a different value.

How do I compare the worksheet ID correctly?

I want to test the readMetadados that makes use of uploadDAO , I want the return to be different according to the ID of the spreadsheet that testo. I expect an error, but I get a nullPointer just because of the DAO mock:

@Bean
UploadDAO getUploadDAO() {
    UploadDAO dao = Mockito.mock(UploadDAO.class);
    File myFile = new File(URL_TEST, "click/T001.json");
    Planilha planilha = new Planilha().setId("invalidFormat").setPath(URL_TEST.concat("click/T001.json"));
    when(dao.getPlanilha(eq(planilha))).thenReturn(myFile);
    return dao;
}

Test

@Test(expected = InternalServerErrorException.class)
public void testReadMetadados_invalidPlanilha_invalidFormat() throws Exception {
    oknok.validacao.entities.Planilha actual = new Planilha().setPath(URL_TEST.concat("click/T001.json"))
                                                                .setId("invalidFormat");
    planilhaReader.readMetadados(actual);
}
    
asked by anonymous 26.05.2015 / 20:42

1 answer

1

It was necessary to refactor the getPlanilha to search for id instead of the entire object

public Planilha getPlanilha(String id) {
        return mongoCollection.findOne("{validacaoId : #, tipo : #}", id, "planilha").as(Planilha.class);
    }

After that, I can get the mocks to return the value according to the received parameter

Testing

@Test(expected = InternalServerErrorException.class)
public void testReadMetadados_invalidPlanilha_invalidFormat() throws Exception {
    oknok.validacao.entities.Planilha actual = new Planilha().setPath(URL_TEST.concat("click/T001.json"))
                                                                .setId("invalidFormat");
    planilhaReader.readMetadados(actual.getId());
}

Mocks

@Bean
UploadDAO getUploadDAO() {
    UploadDAO dao = Mockito.mock(UploadDAO.class);
    File myFile = new File(URL_TEST, "click/T001.json");
    Planilha planilha = new Planilha().setId("invalidFormat").setPath(URL_TEST.concat("click/T001.json"));
    when(dao.getPlanilha("invalidFormat")).thenReturn(myFile);
    return dao;
}
    
26.06.2015 / 16:19