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.