The following seems to be the plan to write a test with DI.
---------------
public class MyJUnit4Test {
@InjectInstrumentation
public Instrumentation mInstrumentation;
@InjectContext
public Context mContext;
@Test
public void testCheck_Preconditions() {
assertNotNull("mInstrumentation cannot be null", mInstrumentation);
assertNotNull("mContext cannot be null", mContext);
}
}
------
I would suggest to change the approach to use the @Inject annotation from JSR330 and have a pre-filled data structure with the object which can be injected. This approach is for example used the Eclipse DI engine which uses the class name as key for the DI.
public class MyJUnit4Test {
@Inject
public Instrumentation mInstrumentation;
@Inject
public Context mContext;
@Test
public void testCheck_Preconditions() {
assertNotNull("mInstrumentation cannot be null", mInstrumentation);
assertNotNull("mContext cannot be null", mContext);
}
}
JSR330 also allows to specify the key, e.g., @Inject @Named("key1"):
The advantage of this approach is IMHO is that you don't need new annotations in case you want to support more types to inject. The data structure Eclipse uses is basically (an enhanced) Map.
Best regards, Lars
P.S.
JSR allows also to use additional qualifier The advantage of this approach is that the DI can be extended. For example you could allow to inject views for the test.
@Inject @View @Named("myid") TextView view;