0

I figured out how to inject non-UI-Elements (Android) into a testclass, using Robolectric and RoboGuice. Unfortunately I was not able to figure out how to inject Android-UI-Elements into a testclass.

Is this possible at the present state of development of RoboGuice and Robolectric?

If it is possible, can somebody give me a hint where to find a tutorial or offer some small code-example? If it is not possible right now a hint will be nice too.

Thanks

Frank
  • 113
  • 2
  • 10

1 Answers1

0

No this is not possible. The InjectView code only accepts one argument which serves as source and destination. So you can't Inject references of views from an activity into another object.

You can check the source for more information: https://github.com/roboguice/roboguice/blob/master/roboguice/src/main/java/roboguice/inject/ViewListener.java#L162

Jeroen
  • 3,399
  • 1
  • 22
  • 25
  • @jtietema ,your answer is referencing InjectView code. But how about not using InjectView ? Is it possible to use Provides method, etc.? – victorwoo Aug 01 '13 at 09:42
  • @victorwoo Wouldn't know from the top of my head. I would think you could, but you would probably have to write a custom annotation. Might run into some scope issues to. – Jeroen Aug 02 '13 at 03:06
  • @jtietema This achieve of module is failed: ` public static WebView webView; @Provides WebView provideWebView() { return webView; } ` The Provides method is called in the beginning and there isn't a chance for WebView to pass in :( – victorwoo Aug 02 '13 at 03:24
  • @victorwoo I don't think the provides annotation is suitable for this. I think you would need to write a custom annotation, but I don't really understand why you would want this in the first place... – Jeroen Aug 02 '13 at 04:37
  • @jtietema I've a lot of objects need to access a single WebView instance. So I want to let RoboGuice to Inject WebView into objects. – victorwoo Aug 02 '13 at 07:17
  • @victorwoo can't think of a way that won't get very messy. You could try letting RoboGuice creating the instance with a provider. Cache the instance in the provider and than Inject that same instance on the other places. But I can imagine that you will run into other problems if you let RoboGuice create your views. (Never tried though) – Jeroen Aug 02 '13 at 12:12