0

I've looked at a lot of posts and read the documentation on Android Instrumentation Tests but cannot get my code to work.

My scenario is that I can't use a mock context object, I need a valid one.

I have tried using:

@Before
public void setUp() throws Exception
{
    super.setUp();

    setActivityInitialTouchMode(true);
    // Injecting the Instrumentation instance is required
    // for your test to run with AndroidJUnitRunner.
    injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    _context = getActivity();

    assertThat(_context, isA(Context.class));
}

But I get

java.lang.RuntimeException: Could not launch activity

I tried having my test extend InstrumentationTest and using getInstrumentation().getContext()

But that is also null.

From what I understand Instrumentation Tests are for exactly that: when you need to utilize the application (i.e. context).

So do you know how I can access a valid, non-null, context object in Android Studio's 2.0 environment with junit4?

Here is my current best attempt:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class StartWorkoutRadialProgressBarTest extends ActivityInstrumentationTestCase2
{
    Context _context;

    public StartWorkoutRadialProgressBarTest()
    {
        super(StartWorkoutRadialProgressBar.class);
    }

    @Before
    public void setUp() throws Exception
    {
        super.setUp();

        setActivityInitialTouchMode(true);
        // Injecting the Instrumentation instance is required
        // for your test to run with AndroidJUnitRunner.
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        _context = getActivity();

        assertThat(_context, isA(Context.class));
    }

    @Test
    public void initialization()
    {
        StartWorkoutRadialProgressBar bar = new StartWorkoutRadialProgressBar(100,100, _context);

        Assert.assertThat(4, is(4));
    }
}

Note: Using context = new MockContext() doesn't work because I get an error the library tries to call a resource.

Aggressor
  • 13,323
  • 24
  • 103
  • 182
  • 1
    "java.lang.RuntimeException: Could not launch activity" -- please show the entire stack trace. Also, FWIW, [this sample project](https://github.com/commonsguy/cw-omnibus/tree/master/Testing/JUnit4) demonstrates using the deprecated `ActivityInstrumentationTestCase2` with JUnit4, along with using `InstrumentationRegistry.getTargetContext()` and `ActivityTestRule`. – CommonsWare May 09 '16 at 20:58
  • 1
    I figured it out just now actually. It was my constructor, it needs to be an activity class (the ActivityInstrumentationTestCase2 doesn't actually provide the context I need) – Aggressor May 09 '16 at 21:06

1 Answers1

0

My constructor was for a class that was not an activity:

public StartWorkoutRadialProgressBarTest()
{
    super(StartWorkoutRadialProgressBarTest.class);
}

When I changed this to just a generic activity class I created and called A_Testing it worked:

public StartWorkoutRadialProgressBarTest()
{
    super(A_Testing.class);
}

So it turns out that you must init with an activity class as the ActivityInstrumentationTestCase2 doesn't provide a valid context object with a getActivity() call.

Aggressor
  • 13,323
  • 24
  • 103
  • 182