-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add extension API for reusable initialization of temporary directories #1786
Comments
Extensions should not depend on other extension - not even on those built-in extensions that Jupiter provides. Why don't you just use |
A generic resource supplier SPI (#1672) could help here, methinks. With such an extension in place, test authors could write: @Test
void test4711(@New(TestDataFolder.class) Path path) { ... } Will try to implement and publish a spike in JUnit Pioneer, soon. Meanwhile, if you're running your tests on Java 11, you may try https://github.com/sormuras/brahms#resource-manager-extension |
That's typically true; however, there are times when interactions between extensions is desirable. For example, the Another option for inter-extension communication/collaboration is the However, for the use case in this issue, allowing fields in an extension to be annotated with
I agree that managing such a temporary directory within the custom extension itself is the more robust approach, especially since the lifecycle of the temporary directory should be managed by that extension and not by the built-in
Yes, we could consider publishing that in something like a new |
Related to #1604. Tentatively slated for 5.5 M2 for team discussion |
Team Decision: Store |
@marcphilipp I was trying to think around how that would work for other extensions. Aren't |
@mkobit, those are valid questions. If an extension attempts to look up the temp dir in the So we probably need to rethink this and provide an on-demand mechanism for acquiring access to the temp dir. One of the potential problems with that is determining what the scope/lifecycle of the temp dir should be. @junit-team/junit-lambda, looks like we need to brainstorm a bit more on this topic. |
Indeed. |
I've been trying to think of some possible ways for clients to use this, as it seems related to my request at #1802. Maybe there could be a notion of "extending the |
contents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
contents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
…ntents Existing tests requiring TemporaryFolders initialise them independently. Many classes, such as JsonAddressBookStorageTest and JsonUserPrefsStorageTest, implement custom methods such as addToTestDataPathIfNotNull(String) method, to dynamically include resources in temporary directories. This causes code duplication. Based on the DRY principle, these duplicated methods can be refactored with the use of a helper class to manage all directory initialisations. Additionally, as we migrate to JUnit 5, TemporaryFolder rules are replaced by TempDir extensions, and there is stronger support for dynamic extensions that can initialise these temporary folders. Thus, it is easier and neater to write an helper class extension that manages the temporary folders. Let's create a common DirectoryInitUtil class that manages the initialisation of and file creation within temporary folders. This has the benefit of ensuring that the temporary directory is properly reset to a known, initialised state, at the start of every test method. Design consideration: It would be ideal to implement this extension in OOP style - abstracting away the idea of copying files and initialising directories by encapsulating them into a ManagedTempDirExtension class. However, this design is not possible because the TempDir tag is not available in extensions [1] as of JUnit 5.4.0. It is slated for JUnit 5.5.0 M2 onwards [2], which is not released at the time of this commit. Future developers can look into converting DirectoryInitUtil into a Object Oriented Class. Notes: this commit only introduces the DirectoryInitUtil extension class. Extensions and the TempDir API can only be used by tests using the JUnit 5 runner. Hence, we can only teach tests to use this DirectoryInitUtil extension later after we migrate tests to use JUnit 5. This ensures that every commit in the meantime remains buildable. [1] https://stackoverflow.com/questions/54647577/how-to-use-tempdir-in-extensions/54651578#54651578 [2] junit-team/junit5#1786
A function that returned a CloseableResource go a long way. Even just making this public: junit5/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java Lines 157 to 159 in 43638eb
No need to do everything by annotations. |
This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. Thank you for your contribution. |
Bump |
I started working on a change that could solve this topic: #2958 |
Now, I'm not saying you should do this at home, but... Since JUnit Jupiter 5.9, it is technically possible to use import static org.assertj.core.api.Assertions.assertThat;
import java.lang.reflect.Method;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.io.TempDir;
class TempDirInExtensionTests {
@Test
@ExtendWith(MyExtension.class)
void test(Path tempDir) {
assertThat(tempDir).asString().contains("junit").endsWith("hack");
}
static class MyExtension implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return parameterContext.getParameter().getType() == Path.class;
}
@Override
public Path resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
try {
Method method = getClass().getDeclaredMethod("getTempDir", Path.class);
return (Path) extensionContext.getExecutableInvoker().invoke(method, this);
}
catch (Exception ex) {
throw new ParameterResolutionException("Failed to resolve @TempDir", ex);
}
}
Path getTempDir(@TempDir Path path) {
return path.resolve("hack");
}
}
} And now I'll show myself out. 😇 |
IMO this request goes against the good design principles and should be discarded. Imagine the impact of this - expecting the It is possible to write extension that depend on other extension, I do it all the time, and I don't need the
|
The original requested goal here seems to have been accomplished with the introduction of Another idea potentially worth exploring for future extensions, is for the extensions themselves to support their own plugin model internally. Instead of requiring generalized support across Jupiter itself, experiment on a single extension and see if the pattern works in a more generalized way. This may overcomplicate the extension and usage, but here is where my brainstorming went for
|
@mkobit for the sake of clarity, |
Coming back to this, while being a bit involved class DemoTests {
@Test
void test(@TempDir(factory = InitializingTempDirFactory.class) Path tempDir) {
// perform test
}
class InitializingTempDirFactory implements TempDirFactory {
@Override
public Path createTempDirectory(AnnotatedElementContext elementContext, ExtensionContext extensionContext)
throws Exception {
Path tempDir = Standard.INSTANCE.createTempDirectory(elementContext, extensionContext);
// initialize tempDir...
return tempDir;
}
}
} A separate public interface TempDirInitializer {
void initializeTempDirectory(Path tempDir, AnnotatedElementContext elementContext, ExtensionContext extensionContext) throws Exception;
} The test class would become: class DemoTests {
@Test
void test(@TempDir(initializer = Initializer.class) Path tempDir) {
// perform test
}
class Initializer implements TempDirInitializer {
@Override
public Path initializeTempDirectory(Path tempDir, AnnotatedElementContext elementContext, ExtensionContext extensionContext)
throws Exception {
// initialize tempDir...
}
}
} |
I have a bunch of tests that require me to initialise a temporary directory with some files, so that the methods in my tests can read and write to it.
Currently, the solution is to implement a BeforeAll/BeforeEach tag and initialise the temporary directory for each test class.
As this is a common routine, it would be more reuse friendly if we could write an extension that encapsulates this routine, and which only needs to receive an input of a Path to initialise a temporary directory with all the files.
I envision the extension to look something like
DirectoryInitExtension.java
MainTest.java
However, as I soon learnt (thanks to a fast response here), TempDirs are not supported in extensions.
I wonder if it is a possibility to enable the same capabilities of TempDirs in extensions.
Deliverables
The text was updated successfully, but these errors were encountered: