Skip to content
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

ApplicationContext not updated in ExtensionContext when rebuildContext = true #756

Open
David-16 opened this issue Apr 28, 2023 · 0 comments

Comments

@David-16
Copy link

David-16 commented Apr 28, 2023

Expected Behavior

In MicronautJunit5Extension, the beforeAll method puts the ApplicationContext into the JUnit ExtensionContext (https://github.com/micronaut-projects/micronaut-test/blob/v3.9.2/test-junit5/src/main/java/io/micronaut/test/extensions/junit5/MicronautJunit5Extension.java#L85)

When rebuildContext = true, then the ApplicationContext is re-assigned to a new pointer (https://github.com/micronaut-projects/micronaut-test/blob/v3.9.2/test-core/src/main/java/io/micronaut/test/extensions/AbstractMicronautExtension.java#L361-L366) but the old pointer is still retained by ExtensionContext

I have my own extension for helping with cross cutting concerns in Micronaut tests and I use the ExtensionContext to get the ApplicationContext. I think this is causing issues for me when I run with rebuildContext = true

See attached reproducing application

The expected behavior is that the ApplicationContext in the extension and the ApplicationContext injected in the test should be the same for each test method

Actual Behaviour

When running the attached application, you can see the ApplicationContext injected into the test differs each time

But the ApplicationContext known by the extension is not updated:

09:46:13.385 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [test]
09:46:13.964 [main] ERROR com.example.MyJUnit5Extension - ApplicationContext = io.micronaut.context.DefaultApplicationContext@4e9658b5
09:46:13.973 [main] ERROR com.example.DemoTest - ApplicationContext = io.micronaut.context.DefaultApplicationContext@4e9658b5

09:46:14.048 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [test]
09:46:14.102 [main] ERROR com.example.MyJUnit5Extension - ApplicationContext = io.micronaut.context.DefaultApplicationContext@4e9658b5
09:46:14.103 [main] ERROR com.example.DemoTest - ApplicationContext = io.micronaut.context.DefaultApplicationContext@65ef722a

09:46:14.141 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [test]
09:46:14.194 [main] ERROR com.example.MyJUnit5Extension - ApplicationContext = io.micronaut.context.DefaultApplicationContext@4e9658b5
09:46:14.195 [main] ERROR com.example.DemoTest - ApplicationContext = io.micronaut.context.DefaultApplicationContext@251ebf23

...

Steps To Reproduce

Run mvn clean test using the attached example application

demo.zip

I was able to hack my way around this but I do not know how best to do it cleanly. The ApplicationContext is recreated by AbstractMicronautExtension which does not know about the JUnit ExtensionContext and there is no easy way to call back to MicronautJunit5Extension

EDIT: I guess maybe the naive way is maybe the best way; simply call store.put(ApplicationContext.class, applicationContext); inside MicronautJunit5Extension.beforeEach every time

Environment Information

JDK 17
Micronaut 3.9.0
Linux

Example Application

https://github.com/micronaut-projects/micronaut-test/files/11355728/demo.zip

Version

3.9.0

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

No branches or pull requests

1 participant