Should we define our interface to make it more verifiable?

For example, fragment A

String readSource(String file); 

fragment B

 String readSource(InputStream in); 

So, if we define the interface as A, then we can only create the file for testing. But after a while it will be difficult to manage the test files. Therefore, using interface B, it will simplify testing. I just need to create a fake InputStream and then check if the method returns the desired result. But we must handle an IOException before we call this method every time.

+4
source share
2 answers

If readSource() does something more complex than just returning the contents of the file, I would definitely choose solution B.

In addition to better testability, the introduction of a new abstract concept ( InputStream ) allows you to clearly distinguish between responsibilities - one object to open the correct file and put its contents in memory, one object to read the source from the stream and return everything you need to return from him (the principle of shared responsibility).

+1
source

If your next TDD, then I would say, to go for a more verifiable approach, this is really a matter of personal preference. A quick thought ... is the code easy to use / maintain when using fragment B? If so, then there are no flaws, and this is definitely a way to go.

+1
source

All Articles