- This project was an idea picked from
sfragisGuice-Junit which enabled DI in fields of test classes in JUnit4, as a customRunner, So the idea was to enable similar functionality usingJUnit5and the removed restrictions such as , test methods should not have parameters, using this this project will enable you to inject services or stubs as method params in yourJUnit5test
-
Your tests should be running on
JUnit5, and you'll need to implement yourself the logic of resolving dependencies as i can't make this choice for your . the demos in the repo might help you get started,demo-cdiuses CDI (Context and Dependency Injection) standard from JAVA EE as aInjectionHandler, anddemo-memoryuses a simple fixedHashMapto do the resolution . -
You should be familiar with the
ServiceLoaderapi, so you should create a filetest/resources/META-INF/services/io.github.chermehdi.junitdi.api.InjectionHandlerProviderand put the fully qualified name of yourInjectionHandlerProviderimplementation so it get's picked by theServiceLoaderat Runtime
public class InMemoryInjectionHandlerProvider implements InjectionHandlerProvider {
@Override
public InjectionHandler createHandler() {
return new MyInjectionHandler();
}
}- Create your implementation
public class MyInjectionHandler implements InjectionHandler {
// Your implementation
}- and now in your tests you just need to add
@ExtendWith(InjectionExtension.class)something like :
@ExtendWith(InjectionExtension.class)
class MyTestClass {
@Test
void testMethod(SomeService service) {
assertNotNull(service);
}
}- or you can use :
@EnableInjection
class MyTestClass {
@Test
void testMethod(SomeService service) {
assertNotNull(service);
}
}- Any bug report or feature request or any kind of contribution is highly appreciated 😃