Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking filesystem

If my unit tests rely on the file system and I need to mock this, what is the best way to go about this?

Thanks

like image 992
theDeveloper Avatar asked Feb 15 '10 14:02

theDeveloper


People also ask

What is mocking a method?

Mocking is done when you invoke methods of a class that has external communication like database calls or rest calls. Through mocking you can explicitly define the return value of methods without actually executing the steps of the method.

What is mocking in spring boot?

Spring Boot's @MockBean Annotation The mock will replace any existing bean of the same type in the application context. If no bean of the same type is defined, a new one will be added. This annotation is useful in integration tests where a particular bean, like an external service, needs to be mocked.

What does mocking mean in Java?

Mocks and stubs are fake Java classes that replace these external dependencies. These fake classes are then instructed before the test starts to behave as you expect. More specifically: A stub is a fake class that comes with preprogrammed return values.


1 Answers

The file system is an excellent example of how TDD can drive you towards a better, more flexible design. Often, when you interact with the file system, you can deal with reading and writing files using Streams or TextWriters instead of actual files.

These are all abstract types and so are easy to mock.

Now you have a more flexible API because it's not tightly coupled to the file system, but still supports file operations.

like image 77
Mark Seemann Avatar answered Oct 05 '22 05:10

Mark Seemann