1

how can I test a complex void method like this one:

public void saveFileToDisk(BtrUploadedDoc uploadedDoc , NewEvenDTO newEvent) {

        try {

        File targetFolder = new File(uploadedDoc.getFilePath()); 
        InputStream inputStream = newEvent.getUploadedFile().getInputstream();
        OutputStream out = new FileOutputStream(new File(targetFolder, uploadedDoc.getUpdoId().toString() + uploadedDoc.getFileExt()));

        int read = 0;

        byte[] bytes = new byte[1024];
        while ((read = inputStream.read(bytes)) != -1) {

            out.write(bytes, 0, read);

        }
        inputStream.close();
        out.flush();
        out.close();
        }
        catch (Exception ex) {
            log.error("Disk write error: " + ex.getMessage(), ex);

        }

    }

Sorry, but I have no idea how to start. I red lot of article but all the tutorials are about simple methods with simple return values. Thank you!

solarenqu
  • 804
  • 4
  • 19
  • 44
  • call your method and then in the test code check that the intended file is in fact on disk – bhspencer Nov 13 '17 at 21:42
  • First thing you need to know is what exactly you're testing and why. If you don't know, then why are you testing it? –  Nov 13 '17 at 21:43
  • This may be helpful : https://stackoverflow.com/questions/3715743/how-can-i-compare-files-in-a-junit-test-case – davidxxx Nov 13 '17 at 21:44
  • You'll need some mocks (see Mockito) to simulate the input args. Then you can check that the right file got created and has the right contents. Better yet, add a factory for input and output streams, then you can test w/o actual file system. –  Nov 13 '17 at 21:45
  • Ok sure, but why should I use junit for that? Why is that Better than I run my application and check if there isn’t any error when I push the button and my file is on the disk? :) sorry for my noob question I’m just trying to understand why is this testing thing is good.. all the tutorial are about adding 1+4 and then assert 5. Ok its really nice but in real we are not writing methods which adding 1+4 :) – solarenqu Nov 13 '17 at 21:46

0 Answers0