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?
– Juliano Alves
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 inwhen(dao.getPlanilha(eq(planilha))).thenReturn(myFile);
– Daniela Morais
@Danielamarquesdemorais you wouldn’t have to mock the spreadsheet either?
– Wellington Avelino
@Wellingtonavelino Spreadsheet entity (class) or the XLSX itself?
– Daniela Morais
@Danielamarquesdemorais Panilha entity.
– Wellington Avelino
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
– Daniela Morais
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
– Daniela Morais
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);
– Isvaldo Fernandes
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.
– Isvaldo Fernandes
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.
– Daniela Morais
@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()
– Daniela Morais
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.– Bruno César
@Danielamarquesdemorais managed to solve?
– Wellington Avelino
@Wellingtonavelino Got it, I will post. If you can give feedbacks or a better resolution.
– Daniela Morais
@Wellingtonavelino I am now having problems with integration tests with Spring and Mocks hahaha
– Daniela Morais
With Spring? What mistake?
– Wellington Avelino
Failed to load Applicationcontext: Beancreationexception, all DAO’s are mocked. I already open another question.
– Daniela Morais