Would it be possible to bind the junit jupiter ExtensionContext into the applicaton injector? #388
Replies: 3 comments 1 reply
-
Thank you for the idea! I will look this later. |
Beta Was this translation helpful? Give feedback.
-
Sorry for delay. In the upcoming version, junit context could be obtained within For example (field declaration): public class MyTest {
@EnableSetup
TestEnvironmentSetup setup = (extension) -> {
// entire junit context (class or method, it depends)
final ExtensionContext context = extension.getJunitContext();
// next you can register hook with required data from context:
extension.hooks((builder)->{
builder.modulesOverride(new MyTestModule(context))
});
}
} Another variant (class declaration): public class MySetup extends AbstractModule implements TestEnvironmentSetup, GuiceyConfigurationHook {
private ExtensionContext context;
@Override
public Object setup(TestExtension extension) {
context = extension.getJunitContext();
// register itself as a hook
extension.hooks(this);
return null;
}
@Override
public void configure(GuiceBundle.Builder builder) {
// register itself as module
builder.modulesOverride(this);
}
// optional
@Override
public void configure(Builder builder) {
builder.modulesOverride(this);
}
@Provides
@Singleton
MyObject provideOverrideSqsClient() {
return MyObjectExtension.get(context);
}
} @TestGuiceyApp(value = App.class, setup = MySetup.class) I suppose this should be enough for your needs. If you want you can easilly bind entire junit context into guice context. |
Beta Was this translation helpful? Give feedback.
-
7.2.0 released. As mentioned before, @EnableSetup
TestEnvironmentSetup setup = (extension) -> {
// entire junit context (class or method, it depends)
final ExtensionContext context = extension.getJunitContext();
extension.hooks((builder)->{
builder.modulesOverride(new MyTestModule(context))
});
} Also, context could be injected as a test method parameter: @BeforeEach
public void setUp(ExtensionContext conte
8000
xt) {
} Also, setup objects were highly improved and now could replace custom junit extensions completely. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes when I'm writing a configuration hook, I like to bind overrrides from a test module. It would be nice if I could obtain data out of the test extension context when writing those provider methods:
Beta Was this translation helpful? Give feedback.
All reactions