Affects: Spring 5.3.8


My scenario:

I'm using Spring Session in my application and the user is required to login (which generates a session) before any API can be called. To simulate this in my unit/integration test, I use a TestExecutionListener to create a session in beforeTestClass/beforeTestMethod and then delete the session in afterTestClass/afterTestMethod. However to actually call the API with the session, I need to access the session ID in the test method itself, but I realized there is no (straightforward) way to pass the session ID from my TestExecutionListener implementation to my test method (do let me know if there is such a way).

So I have switched to use JUnit 5's Extension mechanism instead so I can use ParameterResolver to pass the session ID to the test method by using the method parameter. However using JUnit 5 Extension creates another issue: I can't control the order of my Extension relative to other built-in TestExecutionListener implementations -- for example, I wish to run my Extension after the SqlScriptsTestExecutionListener so the changes from the script specified in @Sql are visible to my Extension.

So I hope the TestContext framework can add the ability to resolve parameters, but I'm not sure if this is possible since not all test frameworks that the TestContext framework supports have the ability to resolve parameters like JUnit Jupiter.

Comment From: sbrannen

Related issues in JUnit

  • https://github.com/junit-team/junit5/issues/2393

Comment From: sbrannen

However to actually call the API with the session, I need to access the session ID in the test method itself, but I realized there is no (straightforward) way to pass the session ID from my TestExecutionListener implementation to my test method (do let me know if there is such a way).

I don't think there is any easy way to achieve that.

If you want a TestExecutionListener to provide something for a test method, I believe you only have the following options.

  1. store the value in a ThreadLocal variable that your TestExecutionListener makes available to the test method -- for example, via a public static method.
  2. implement the TestExecutionListener so that it performs dependency injection explicitly for the session ID -- either via field injection or setter injection.
  3. Implement something like org.springframework.test.context.event.ApplicationEventsTestExecutionListener.registerListenerAndResolvableDependencyIfNecessary(ApplicationContext) which registers a test-scoped or thread-scoped bean that can be @Autowired into the test.

Related Issues

  • 18606

However using JUnit 5 Extension creates another issue: I can't control the order of my Extension relative to other built-in TestExecutionListener implementations -- for example, I wish to run my Extension after the SqlScriptsTestExecutionListener so the changes from the script specified in @Sql are visible to my Extension.

That's correct: you cannot influence the execution order between JUnit Jupiter extensions and Spring TestExecutionListener implementations, since the SpringExtension is a Jupiter extension that invokes all registered Spring TestExecutionListener implementations.

In any case, please let me know if one of the aforementioned workarounds works for you, and if none of them do I'll see if I can come up with another solution.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.