Reputation: 29
My void method writes the matter into a file.How can I write a test method for it?
Upvotes: 1
Views: 109
Reputation: 272707
Ideally, use dependency injection. Don't have your method create a file, have it take a Writer (or PrintWriter or whatever) instance. That way, you can have your test inject a mock Writer that allows it to capture the data being written.
If you aren't at liberty to modify your method, then you'll have to very carefully use your test class's @Before
/@After
methods to create an isolated environment for the file to be created in.
Upvotes: 9