I am trying to write Unit tests for activity methods using ActivityUnitTestCase. But always i am getting null pointer exception while calling startActivity()
. Below is my code.
I debugged and found this always returning null getInstrumentation().getTargetContext()
. But i don't really understand whats happening here.
CODE
public class ScoreBoardActivityTest extends ActivityUnitTestCase<ScoreBoardActivity> {
public ScoreBoardActivity activity;
public ScoreBoardActivityTest() {
super(ScoreBoardActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
startActivity(new Intent(getInstrumentation().getTargetContext(), ScoreBoardActivity.class), null, null);
activity = getActivity();
}
@Test
public void testActivityNotNull(){
Assert.assertNull(activity);
}
@Test
public void testBatsmanOneAddRunButtonClickUpdateTotalRuns(){
activity.onBatsmanOneAddRunClick(null);
Assert.assertEquals(activity.totalRuns, 1);
}
}
What i am doing wrong here?
Is there anything else i need to set it up?