Skip to content

Conversation

@MSattrtand
Copy link
Collaborator

Resolves #79

public abstract class AbstractSpringTest {

@TempDir
static protected Path tempDir;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? static?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue is with BasePersistenceFactory. Because it is annotated with @configuration, it gets initialised before we can create a temporary folder and set it as the source for BasePersistenceFactory.

The only reliable way we found to inject a temporary directory is via @DynamicPropertySource. However, this method requires the property to be static. As a result, the temporary directory also has to be static, which is counterintuitive when we want isolated temporary folders for each test.

One workaround is to set a static temporary folder as the initial source so that BasePersistenceFactory does not attempt to create its own directories, and then override this value in @beforeeach with a new temporary folder. This approach works, but in some tests it leads to desynchronization between the current working directory and the one registered earlier. While this can be solved by adjusting the affected tests, it makes the setup less intuitive and harder to maintain.

Overall, I have this solution coded and can push it, but I'm not sure if it would be worth it.

import java.nio.file.Path;

@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this ?


@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class AbstractSpringTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to extend every test with this ?

@MSattrtand MSattrtand force-pushed the refactor-tests branch 2 times, most recently from 91a706c to c5a4f80 Compare November 25, 2025 13:04
@MSattrtand
Copy link
Collaborator Author

In short, here’s the solution:

  • Added two new config classes: RepositoryPathConfig and ScriptPathConfig.
  • They manage the parameters: scriptPath, rdf4j.RepositoryUrl, and rdf4j.RepositoryName.
  • In production, these values are still read from application.properties (no change in behaviour), but are now exposed as Beans.
  • In the test environment, they create temporary folders, handle their cleanup, and expose them as Beans.
  • The rest of the code now consumes the injected beans (scriptPath, rdf4jRepositoryUrl, rdf4jRepositoryName) instead of reading application properties directly.

This cleanly separates repository handling logic between prod and test, while keeping the overall design consistent and minimally changed.

Additionally:

  • We no longer need @DirtiesContext in tests.
  • Tests won’t fail due to leftover directories being reused as repositories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor tests to use TempDir from JUnit 5

3 participants