How can you use roboguice and robolectric to test a fragment in a library?
I would like to unit test a fragment that lives in an android library. The problem I am having is that the views are null.
e.g.
java.lang.NullPointerException: Can't inject null value into class com.mypackage.MyFragment.txtPublishedTime when field is not @Nullable
at roboguice.inject.ViewListener$ViewMembersInjector.reallyInjectMemberViews(ViewListener.java:220)
at roboguice.inject.ViewListener$ViewMembersInjector.reallyInjectMembers(ViewListener.java:189)
at roboguice.inject.ViewListener$ViewMembersInjector.injectViews(ViewListener.java:294)
at roboguice.inject.ContextScopedRoboInjector.injectViewMembers(ContextScopedRoboInjector.java:259)
at roboguice.fragment.RoboFragment.onViewCreated(RoboFragment.java:24)
at com.mypackage.MyFragment.onViewCreated(MyFragment.java:193)
When I add a 'Nullable' annotation to the fields I don't get the above exception, but all the views on my fragment are null
When I try calling injectViewMembers(myFragment)
that generates a NPE.
Since library R file id's are not final I used tags for view injection - not sure if this has anything to do with it.
What do I need to do so that this unit test can pass?
MyFragment
public class MyFragment {
@InjectView(tag="scrollViewContainer") ScrollView scrollView;
@InjectView(tag="imgLead") ImageView imgLead;
@InjectView(tag="imgPlayShow") ImageView imgPlayShow;
@InjectView(tag="flHsvTags") FlowLayout tagsContainer;
@InjectView(tag="txtPublisher") TextView txtPublisher;
@InjectView(tag="txtTime") TextView txtPublishedTime;
@Inject VolleyHelper volleyHelper;
@Inject ConfigurationHelper configurationHelper;
@Inject UrlHelper urlHelper;
@Inject AnalyticsHelper analyticsHelper;
---
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment_layout, container, false);
return view;
}
---
Test
@Config(constants = BuildConfig.class, sdk=21)
@RunWith(RobolectricGradleTestRunner.class)
public class MyFragmentTest {
static {
RoboGuice.setUseAnnotationDatabases(false);
}
@Mock UrlHelper urlHelper;
@Mock ConfigurationHelper configurationHelper;
@Mock AnalyticsHelper analyticsHelper;
@Mock VolleyHelper volleyHelper;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
@Test
public void shouldNotBeNull() throws Exception
{
MyFragment myFragment = new MyFragment();
RoboGuice.overrideApplicationInjector(RuntimeEnvironment.application, new Module() {
@Override
public void configure(Binder binder) {
binder.bind(VolleyHelper.class).toInstance(volleyHelper);
binder.bind(UrlHelper.class).toInstance(urlHelper);
binder.bind(AnalyticsHelper.class).toInstance(analyticsHelper);
binder.bind(ConfigurationHelper.class).toInstance(configurationHelper);
}
});
final RoboInjector injector = RoboGuice.getInjector(RuntimeEnvironment.application);
injector.injectMembers(myFragment);
//tried this but it generates a NPE
//injector.injectViewMembers(myFragment);
SupportFragmentTestUtil.startVisibleFragment(myFragment);
assertNotNull(myFragment.getView());
}
}
Aucun commentaire:
Enregistrer un commentaire