Test drive for androids - getinstrumentation () returning null

I am trying to make a test case extending the intstrumentationtestcase, and whenever I call getinstrumentation (), it returns a null instance of Instrumentation instead of Instrumentation, providing any automation I want to make useless. I have the permission set in the manifest, even if I only test automation in the same application, that this case will work ... any ideas?

+5
source share
4 answers

You need to enter a software program software by calling injectInstrumentation(InstrumentationRegistry.getInstrumentation());with the help InstrumentationRegistryof the official All Android-testing-support-lib: 0.1

+5

, , getInstrumentation() setUp (InstrumentationTestCase). LogCat:

import android.app.Instrumentation;
import android.test.InstrumentationTestCase;
import android.util.Log;

public class TestInstrumentation extends InstrumentationTestCase {

private static final String LOG_TAG = BrowseLocationsTest.class.getSimpleName();

private Instrumentation instr;

public TestInstrumentation() {
    instr = getInstrumentation();
    Log.d(LOG_TAG, "TestInstrumentation instrumentation: " + instr);
}

@Override
protected void setUp() throws Exception {
    instr = getInstrumentation();
    Log.d(LOG_TAG, "setUp instrumentation: " + instr);

    super.setUp();

    instr = getInstrumentation();
    Log.d(LOG_TAG, "setUp instrumentation: " + instr);
}

public void testInstrumentation() {
    Log.d(LOG_TAG, "testInstrumentation instrumentation: " + instr);
}
}

, super.setUp().

+3

@RunWith (AndroidJUnit4.class), , setUp(), @Gallal @Dariusz Gadomski, NullPointerExceptions.

It turns out that I forgot to include the annotation @Beforein my setup method, so jUnit4 did not run it, whereas before using jUnit3-based tool tests, it was run. Since I implemented the toolkit injection in setUp (), it was never entered, although the code looked as if it was entering it.

So instead

@Override
protected void setUp() throws Exception {
...

Be sure to use

@Before
public void setUp() throws Exception {
    super.setUp();
    injectInstrumentation(InstrumentationRegistry.getInstrumentation());
}

instead.

+2
source

I think you really need Context, in JUnit4 we can get context InstrumentationRegistry.getContext();

0
source

All Articles