3rd party extensions integration¶
It is extremely simple in JUnit 5 to write extensions. If you do your own extension, you can easily integrate with guicey or dropwizard extensions.
Tip
In many cases, it would be easier to write a custom guicey setup object which provides almost the same abilities as junit extensions plus guicey awareness. All field-based extensions in guicey are implemented with setup objects.
Guicey side¶
If you already have a junit extension that stores something in ExtensionContext
then you can:
- Bind value into configuration directly:
.configOverrideByExtension(ExtensionContext.Namespace.GLOBAL, "ext-key", "config.key")
- Bind junit
ExtensionContext
as test method parameter (and access storage manually):@BeforeAll public static void beforeAll(ExtensionContext junitContext) { ... }
- Inside setup object access junit context:
public class MyExt implements GuiceyEnvironmentSetup { @Override public Object setup(TestExtension extension) throws Exception { ExtensionContext context = extension.getJunitContext(); } }
Extension side¶
There are special static methods allowing you to obtain main test objects:
GuiceyExtensionsSupport.lookupSupport(extensionContext)
->Optional<DropwizardTestSupport>
GuiceyExtensionsSupport.lookupInjector(extensionContext)
->Optional<Injector>
GuiceyExtensionsSupport.lookupClient(extensionContext)
->Optional<ClientSupport>
For example:
public class MyExtension implements BeforeEachCallback {
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Injector injector = GuiceyExtensionsSupport.lookupInjector(context).get();
...
}
}
(guicey holds test state in junit test-specific storages and that's why test context is required)
Warning
There is no way in junit to order extensions, so you will have to make sure that your extension
will be declared after guicey extension (@TestGuiceyApp
or @TestDropwizardApp
).
There is intentionally no direct api for applying configuration overrides from 3rd party extensions because it would be not obvious. Instead, you should always declare overridden value in extension declaration. Either use instance getter:
@RegisterExtension
static MyExtension ext = new MyExtension()
@RegisterExtension
static TestGuiceyAppExtension dw = TestGuiceyAppExtension.forApp(App.class)
.configOverride("some.key", ()-> ext.getValue())
.create()
Or store value inside junit store and then reference it:
@RegisterExtension
static TestGuiceyAppExtension app = TestGuiceyAppExtension.forApp(App.class)
.configOverrideByExtension(ExtensionContext.Namespace.GLOBAL, "ext1")
.create();