Repository navigation
Avoid a checked IOException in field initializers in TemporaryFolderToTempDir - #1280
Merged
Merged
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
TemporaryFolderToTempDirturns aTemporaryFolderfield without@Ruleor@ClassRuleinto aFileinitialized withFiles.createTempDirectory("junit").toFile(). That method throws the checkedIOException, which a field initializer cannot throw, so the class no longer compiles.Before:
After (does not compile: unreported exception
IOException):This surfaced while validating the Spring Boot 4.1 migration over open source Spring Boot 3.x repositories, in xm-online/xm-ms-config:
LocalJGitRepositoryConfiguration, a Spring@Configurationin the test sources, holds three plainTemporaryFolderfields and callscreate()on them itself.Skipping such fields, or the file, would move the problem: the file also declares
public statichelpers that take aTemporaryFolder, andJGitRepositoryIntTestpasses its@Rule TemporaryFolderfields to them. That test is converted to@TempDir Fileeither way, so the helpers' parameters have to becomeFiletoo.This change keeps the conversion, and for a field initializer in Java it calls a private static helper that the recipe adds to the class:
When the class already has a no-argument method of that name, the helper is called
createTempDirectory2(and so on). Local variables, arguments and Groovy are unchanged.Is this the right change?
TemporaryFolderfield is a common way to get a temporary directory outside a rule, and the result does not compile.File, as before.@Rule/@ClassRulefields still become@TempDirwithout an initializer. The directory is created when the field is initialized rather than atcreate(), which the recipe already removed.Tests:
TemporaryFolderToTempDirTest: newfieldNotAsRule(instance and static fields,create()andgetRoot()in methods and on a parameter; failed before) andruleFieldNextToPlainFieldAndExistingHelperName(a@Rulefield next to a plain one, with an existingcreateTempDirectory()).TemporaryFolderToTempDirTest,JUnit5MigrationTest,UseWiremockExtensionTest,HandleExternalResourceRulesTest,RemoveJupiterMigrationSupportTest,TimeoutRuleToClassAnnotationTestandMockitoJUnitToMockitoExtensionTestpass.