1

When attempting to build an example ActivityUnitTestCase around the Navigation Drawer example found on the Android developer site [link], getActionBar() is returning null during MainActivity.onCreate(). I have had similar problems with this test case when using Fragments in the given activity. While I have not seen it explicitly stated anywhere, it appears as though particular facets of the UI are not supported in this type of test.

Some documentation around those specific limitations would be helpful. I have not found anything of the sort on either developer.android.com or stackoverflow.com.

My Constraints

I am using Dagger to perform injection in my Activity's onCreate method, which effectively rules out ActivityInstrumentationTestCase2. That test case does not allow for mock Applications or Contexts, the former of which is being used during injection.

What should I explore for testing, given that I need pre-creation dependency injection without UI test case limitations?

jneander
  • 1,150
  • 14
  • 34
  • Have you figured out a solution to these problems? I am having similar issues. – Catalin Morosan Sep 02 '14 at 09:05
  • I do not recall if or how this particular problem was solved. You might consider extracting as much of your behavior into custom, testable classes and having it communicate with your Activity/Fragment through an interface. This would allow you to decouple the business logic from the Android UI classes, which greatly limit control (at least in testing). – jneander Sep 02 '14 at 13:46
  • 1
    I solved my issue by switching to ActivityInstrumentationTestCase2 and setting the test object graph in setUp so that my activities get injected with the right objects. – Catalin Morosan Sep 03 '14 at 14:51

0 Answers0