You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Interesting explanation from claudio about why def vs final matters here:
Yes — it's a consequence of how Spock restructures the method bytecode to support cleanup:.
Spock's AST transformation wraps the test body in a try/finally so that cleanup: always runs, even when the test throws. To make a variable declared in given: reachable inside that finally block, Spock has to hoist the declaration to the top of the method (outside the try), separate from the assignment:
That two-step pattern — declare-then-assign — is illegal for final variables. A final local must be assigned exactly once, at its declaration site. So when Spock tries to emit final bucketName = null at the top of the method and bucketName = createBucket() inside the try, the compiler rejects it with "might not have been initialized" (or a similar definite-assignment error).
With def, the same split is fine because the variable is just a nullable reference with no assignment-once constraint.
The reason this was invisible before your change: the original test had no cleanup: block, so Spock never needed to hoist bucketName outside the try — it stayed a normal local variable in the given: scope and final was harmless there.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Fixes an S3 unit test that creates a bucket without deleting it on cleanup