I’ve been using Roboletric for about a year now on a client project and we recently ran into an issue when we started testing a new screen. We use Roboguice for dependency injection and when the view would get created under test the views that would get injected at normal runtime would not get injected during test. All of a sudden the injected view would be null.
The class looked like this:
The were trying to inject the LinearLayout and it was not working. The view was always null.
The view looked like this:
Notice the “a” in the “xmlns:a=” in the view. We changed it from the default “xmlns:android=”, doing this caused the injected views to be null. Why? Robolectric does not support anything but the default android namespacing in your layouts.
Therefore, always use “xmlns:android=” as your namespace declaration for your views if you’re using Robolectric and Roboguice.
Update: Thanks to Mike Burton, he pointed out that this is only the case for if you’re using “findViewById” and you’re using Robolectric. This has no correlation to Roboguice. We’re simply using Roboguice in my clients project and thought I’d make note of it. Roboguice is not doing anything it should not be doing.