Reputation: 21
I have a function that checks to see if a file exists or not. If it does exist, it proceeds to execute some code. It's this code that I need to test.
I cannot have an actual file in the test suite, so Im attempting to mock Files.exists()
to have it return true.
The function looks similar to this
protected function foo() {
Path jsonPath = getPath();
if (Files.exists(jsonPath)) {
// Code to test...
}
}
Upvotes: 0
Views: 1988
Reputation: 794
If context of your function is class, just create wrapper for Files.exists
and mock it in tests. That way you will be able to check your functionality, unfortunately at the same time the only way to test newly created class is thru integration tests without mockito.
class CheckIsFileExist{
operator fun invoke(jsonPath: String) = Files.exists(jsonPath)
}
Upvotes: 1
Reputation: 21
As suggested in the comments of the thread, testing the functionality using a testing file system was a better approach than attempting to mock the exists() function without resorting to some major changes to the class structure.
Upvotes: 1