What is the purpose of unit testing in this scenario if the layout is used

I have a question about unit testing. I thought about this for several days and could not find the answer.

Let's say I have a function that downloads files from a web service and writes it to disk. What is the purpose of testing it without actually viewing files downloaded and written to disk?

Although I understand that we can mock a web service, but how can I do this in this scenario? As far as I know, something like Mockito only mocks behavior, but does not provide meaningful data (file in this example).

Even if we can scoff at the behavior of a web service, how can I scoff at writing a file to disk without writing anything to disk?

Sorry, I do not have a specific example for this. Please feel free to give your opinion.

Many thanks

+3
source share
2 answers

I always say that unit testing your code makes it more modular, as it makes you think how I can separate this class and test it in isolation.

In your case, when you want to test something like writing a file to disk, you can instead force your method to write a file to interact with simply OutputStream. This ensures that you can just pass ByteArrayOutputStreamin instead of the actual one. FileOutputStreamThen you can convert the bytes to String and check if the content matches what you really expect.

FileOutputStream temp , .

OutputStream, , FileOutputStream ..

, , - - . , , , - , - .

+5

, , - . , ?

. . ( , , ) . , - . .

, , / , - , .

+4

All Articles