InstrumentationRegistry is an exposed registry instance that holds a reference to the instrumentation running in the process and it’s arguments and allows injection of the following instances:
InstrumentationRegistry.getInstrumentation(), returns the Instrumentation currently running.
InstrumentationRegistry.getContext(), returns the Context of this Instrumentation’s package.
InstrumentationRegistry.getTargetContext(), returns the application Context of the target application.
InstrumentationRegistry.getArguments(), returns a copy of arguments Bundle that was passed to this Instrumentation. This is useful when you want to access the command line arguments passed to Instrumentation for your test.
@RunWith(AndroidJUnit4.class) @SmallTest public class LogHistoryAndroidUnitTest {
public static final String TEST_STRING = "This is a string"; public static final long TEST_LONG = 12345678L; private LogHistory mLogHistory;
//前处理 @Before public void createLogHistory() { mLogHistory = new LogHistory(); }
@Test public void logHistory_ParcelableWriteRead() { // Set up the Parcelable object to send and receive. mLogHistory.addEntry(TEST_STRING, TEST_LONG);
// Write the data. Parcel parcel = Parcel.obtain(); mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());
// After you're done with writing, you need to reset the parcel for reading. parcel.setDataPosition(0);
// Verify that the received data is correct. assertThat(createdFromParcelData.size(), is(1)); assertThat(createdFromParcelData.get(0).first, is(TEST_STRING)); assertThat(createdFromParcelData.get(0).second, is(TEST_LONG)); } //后处理 @After }
TestCase
继承 android.test 包下面一系列 xxxxTestCase。 IDEA 自动生存的例子: