82827

Robolectric: actionBar.hide() returns Null

Question:

I'm new to Android unit testing and I'm using Robolectric as a testing framework. I use Robolectric 2.2.

I'm trying to test an activity which is like that :

public class LoginActivity extends SherlockActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); ActionBar abs = getSupportActionBar(); abs.hide(); }

and here's my test class :

@RunWith(RobolectricTestRunner.class) public class LoginActivityTest { @Before public void setUp() throws Exception { System.setProperty("dexmaker.dexcache", "/sdcard"); activity = Robolectric.buildActivity(LoginActivity.class).create().get(); } @Test public void should_loginActivity_created() throws Exception { assertNotNull(activity); }

I'm getting this error :

java.lang.NullPointerException at auth.LoginActivity.onCreate(LoginActivity.java:119)

This line refers to abs.hide();

NOTE : I tried <a href="https://gist.github.com/xian/5052673" rel="nofollow">Xian's Gist</a> and it did not work.

Also I try to create ShadowSherlockActivity like <a href="https://gist.github.com/ersentekin/7689335" rel="nofollow">this</a> But I have no idea how to use this shadow class to create activity like :

activity = Robolectric.buildActivity(LoginActivity.class).create().get();

NOTE 2 : I try to use Robolectric Snapshot 2.3 but it did not solved.

Thanks.

Answer1:

As @ersentekin and I worked out in the comments, here is a <a href="https://gist.github.com/justinmuller/8025465" rel="nofollow">Gist</a> that takes @Xian's <a href="https://gist.github.com/xian/5052673" rel="nofollow">Gist</a> with a suggestion from <a href="https://gist.github.com/marsucsb/6059760" rel="nofollow">marsucsb</a> and modifies it to work with Robolectric 2.2+

Answer2:

Have u added the android:theme in your androidmanifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="false" android:theme="@style/Theme.Sherlock">

Recommend

  • Android Fullscreen Not Working: Gingerbread
  • onOptionsItemSelected isn't being called
  • Finding the com… to use in Android Layout
  • Action bar is hidden and that shown instantly after that
  • Android unable to change background color of ActionBar
  • Material Design support below Lollipop - crashes
  • ActionBarSherlock and custom TextBox (extends AutoCompleteTextView)
  • This SlidingMenu appears to already be attached Android
  • How to animate home button in action bar?
  • Why can't I inject this javascript in the webview on android?
  • android actionbar unit test using ActivityInstrumentationTestCase2
  • Programmatically set ActionBarTab background color with tab selector line color at bottom with user
  • Android, How to theme font colour for just TextView Widgets?
  • AutoHotKey JavaScript Link
  • How to delete item/file from a listView and external SD?
  • Customize back navigation
  • Share Invoke is sometimes blank
  • Testing with Robolectric in Eclipse
  • EasyMock - mock object returned from new Object
  • I need action bar tabs with custom title bar android
  • ActionBar and Fragments on ICS 4.0.3
  • JUnit assumptions fail Theories
  • Icon's priority on action bar (keeping order of items)
  • How do I customize menu item's background color?
  • Regex, extract string not between symbols
  • Proper Mutational scale value in matplotlib
  • Mockito.when().thenReturn() doesn't work or returns null
  • How can I make a circular menu icon? Please see details
  • Spring Boot integration tests doesn't read properties files
  • Is it mandatory to have a doGet or doPost method?
  • I have two activity.I want to Send the data from second activity to previous activity.First activity
  • matlab crashes without dump file when using fopen for file
  • Prevent page break in text block with iText, XMLWorker
  • Python 3.2.2, error(scripts to exe)
  • NSIS decompiler
  • Authentication failed with Azure Active Directory in Windows Phone
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • Problem while Building a Setup Project for a windows Service?
  • How to use RequestBodyAdvice
  • Array.prototype.includes - not transformed with babel