1

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?

vinothp
  • 9,939
  • 19
  • 61
  • 103
  • Change `getInstrumentation().getTargetContext()` to `getInstrumentation().getTargetContext().getApplicationContext();` – piotrek1543 Jan 11 '16 at 23:18
  • if not working: http://stackoverflow.com/questions/5544205/accessing-application-context-from-testsuite-in-setup-before-calling-getactivi – piotrek1543 Jan 11 '16 at 23:19
  • @piotrek1543 thanks for your comment.but its not working any more .. – vinothp Jan 12 '16 at 21:41

1 Answers1

0

I made a note for this before for ActivityUnitTestCase:

// This will always be null with extends ActivityUnitTestCase<>
// home = getActivity();

You can get [to] the context by doing this:

public class ScoreBoardActivityTest extends ActivityInstrumentationTestCase2<ScoreBoardActivity> {

    private ScoreBoardActivity scoreBoardActivity;

    public ScoreBoardActivityTest() {
        super(ScoreBoardActivity.class);
    }

    // Called before every test case method
    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Use here to start the activity anew before each test
        // Use in test methods should re-start the activity each time called or bring it back after
        // finishing or intents
        scoreBoardActivity = getActivity();
    }

Another note is that ActivityUnitTestCase is for VERY basic testing. Its nearly useless imo and not something I care about using.

The ActivityInstrumentationTestCase2 is what allows use of the activity and is started with getActivity() if I remember correctly.

If you want to specifically see context it then might look like:

scoreBoardActivity.getApplicationContext()

But you can just use scoreBoardActivity.

Also note that tests are alphabetical so your testActivityNotNull() could get behind other tests such as testAcorn or testAbat.

CmosBattery
  • 934
  • 7
  • 13
  • Thanks for your answer. I tried this as well. The odd thing happening with this one is the test always get passed. Do you have any idea? – vinothp Jan 12 '16 at 21:37