How to return the value according to the received parameter?

Asked

Viewed 200 times

2

I had to mock with DAO, but the method receives a spreadsheet object and according to the id attribute of the Spreadsheet I need to return a different value.

How do I do this to correctly compare the spreadsheet ID?

I wish to test the readMetadados who makes use of the uploadDAO, wish the return will be different according to the ID of the spreadsheet I test. I expect an error, but I get one nullPointer precisely 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;
}

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);
}
  • Your code is confused. What are you trying to test?

  • I’m testing readMetadados, this uses the uploadDAO and search for the file. However, according to the id of the sending spreadsheet, I want the mockite to return a different file. I don’t understand how to do this in when(dao.getPlanilha(eq(planilha))).thenReturn(myFile);

  • @Danielamarquesdemorais you wouldn’t have to mock the spreadsheet either?

  • @Wellingtonavelino Spreadsheet entity (class) or the XLSX itself?

  • @Danielamarquesdemorais Panilha entity.

  • How would you mock the spreadsheet? It’s just a get and set class, which I mount in the test only to pass by parameter

  • Taking advantage, is it good practice to use a ready-made spreadsheet (external file) to test? I mean the xlsx file, is located inside the project folder

  • What is the purpose of @Bean ? ’s instance of dao is linked in what way with the instance used by the worksheet class? The mockite returns nullPointer because you are picking up from the method that should query some list of Files objects and swapping for a single file, I think you need to simulate this list when(dao.getPlanilha(eq(spreadsheet))). thenReturn(Array);

  • On the issue of using external file, I took a test course of Caelum, well the tests need to be fast, you can simulate the entry of some file or connection with the bank, but devote only 1 test for this purpose, if every test query a file or connect in the bank, you will be doing several times 1 redundant test.

  • Bean is about Spring’s Autowired. I resolved this issue by passing the url of the file path to the mockite and so that it returns each specific file.

  • 1

    @Isvaldofernandes What I find confusing is that the class is precisely for reading spreadsheet, using Apache POI. I had to mock but returned a New File()

  • I’m not sure I understand your question, but all you have to do is create another when... to another file and spreadsheet, right? If you don’t want to create everything by hand, always use fake Objets. This library is legal for this. If possible, try to include the stack trace of NPE and also the minimum of the method that generates Exception, in order to be able to reproduce. The cool thing would be for you to include not things from your project, but something minimal that reproduces the problem you’re having. I tried to play here, but I don’t think that’s the answer you want.

  • @Danielamarquesdemorais managed to solve?

  • @Wellingtonavelino Got it, I will post. If you can give feedbacks or a better resolution.

  • @Wellingtonavelino I am now having problems with integration tests with Spring and Mocks hahaha

  • With Spring? What mistake?

  • Failed to load Applicationcontext: Beancreationexception, all DAO’s are mocked. I already open another question.

Show 12 more comments

1 answer

1


It was necessary to refactor the getPlanilha to make a search for id instead of the whole object

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

After that, I can make mocks 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;
}

Browser other questions tagged

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