Add CloseableFakeSftpServer extends FakeSftpServer #11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The class can be instantiated, configured and closed manually, if doing everything within a single lambda expression is undesirable for a given use case. In order to achieve that, some methods and fields had to be made
protected
(fromfinal
).For example, in BDD (behaviour-driven development) users want to test in a given-when-then fashion, i.e. separate the stimulus to the system under test (when) from verifying results later (then). Some test frameworks like Spock even require separate blocks, making the use of a single lambda feel somewhat unnatural and clunky there. See also this Stack Overflow answer.
Because the class implements
AutoCloseable
, it can be optionally also be instantiated in a try with resources structure.A parametrised Spock (Groovy) example test looks like this:
With the original
FakeSftpServer
, everything that is now cleanly separated by concern in thegiven:
,and:
,expect:
blocks would be done within a single lambda expression, only for the sake of resource management, which is now handled via@AutoCleanup
for the server field definition. As an alternative, in other test frameworks,server.close()
could be called in a tear-down method running after the test or by using try with resources within the test.