I'd be very grateful for your comments on whether this looks right to you - I am just getting started in this area and it's entirely possible that I'm missing something.
This is probably getting ahead of myself, but there's quite a bit of duplication between this and RoboUnitTestCase which should probably be DRYed up before this, or something like it, is integrated into RoboGuice:
Start of code--------------------------------------
package roboguice.test;
import roboguice.application.RoboApplication;
import roboguice.service.RoboService;
import roboguice.inject.ContextScope;
import android.content.Context;
import android.test.ServiceTestCase;
import com.google.inject.Injector;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
public class RoboServiceTestCase<ServiceType extends RoboService, AppType extends RoboApplication> extends ServiceTestCase<ServiceType> {
protected Injector injector;
public RoboServiceTestCase(Class<ServiceType> serviceClass) {
super(serviceClass);
}
@Override
protected void runTest() throws Throwable {
final Context context = getContext();
final Constructor constructor = applicationType().getConstructor(Context.class);
final RoboApplication app = (RoboApplication)constructor.newInstance(context);
injector = app.getInjector();
final ContextScope scope = injector.getInstance(ContextScope.class);
setApplication(app);
try {
scope.enter(context);
super.runTest();
} finally {
scope.exit(context);
}
}
protected Injector getInjector() {
return injector;
}
protected Class<? extends RoboApplication> applicationType() {
final ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
return (Class<? extends RoboApplication>) parameterizedType.getActualTypeArguments()[1];
}
}
End of code-----------------------------------
--
paul.butcher->msgCount++
Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?
http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher
--
You received this message because you are subscribed to the Google Groups "roboguice" group.
To post to this group, send email to robo...@googlegroups.com.
To unsubscribe from this group, send email to roboguice+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/roboguice?hl=en.
I.e. could RoboUnitTestCase lose its runTest method and instead have:
> Context context;
> ContextScope scope;
>
> @Override
> protected void setUp() throws Exception {
> context = getInstrumentation().getTargetContext();
> final Constructor constructor = applicationType().getConstructor(Context.class);
> final RoboApplication app = (RoboApplication)constructor.newInstance(context);
> injector = app.getInjector();
> scope = injector.getInstance(ContextScope.class);
>
> scope.enter(context);
> }
>
> @Override
> protected void tearDown() throws Exception {
> scope.exit(context);
> }
(or something along those lines)?
Seems to, yup.
I'll add this to the list of things to do when I put my patch together.