Instrumented Test - abdulmukit98/techshopJU GitHub Wiki
Instrumented test allow test case to control the life cycle and user interaction event.
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test:core:1.4.0'
    androidTestImplementation 'androidx.test:rules:1.4.0'
in AndroidTest package
- Create ActivityTestRule to access our Activity which is now testing
    @Rule
    public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
- Create test case to pass our Activity's method for instrumented testing
/**
     * Activity testing
     * OnCreate method testing
     *
     * An object of MainActivity is created to perform testing
     * assertThat help to match our view with given one.
     * assertEquals only check for return type, wher assertThat work API level
     */
    @Test
    public void onCreateInstrumented() throws Exception
    {
        MainActivity mainActivity = rule.getActivity();
        RadioGroup rgLayer = mainActivity.findViewById(R.id.rgroupLayer);
        assertThat(rgLayer, notNullValue());
        assertThat(rgLayer, instanceOf(RadioGroup.class));
        RadioGroup rgMasking = mainActivity.findViewById(R.id.rgroupMasking);
        assertThat(rgMasking, notNullValue());
        assertThat(rgLayer, instanceOf(RadioGroup.class));
        Button btnAdd = mainActivity.findViewById(R.id.btnAdd);
        assertThat(btnAdd, instanceOf(Button.class));
        Button btnSub = mainActivity.findViewById(R.id.btnSub);
        assertThat(btnSub, instanceOf(Button.class));
        Button btnUpload = mainActivity.findViewById(R.id.btnUpload);
        assertThat(btnUpload, instanceOf(Button.class));
        Button btnCart = mainActivity.findViewById(R.id.btnCart);
        assertThat(btnCart, instanceOf(Button.class));
        EditText edtWidth = mainActivity.findViewById(R.id.edtWidth);
        assertThat(edtWidth, instanceOf(EditText.class));
        EditText edtHeight = mainActivity.findViewById(R.id.edtHeight);
        assertThat(edtHeight, instanceOf(EditText.class));
        DatabaseReference databaseReference = mainActivity.databaseReference;
        assertThat(databaseReference, notNullValue());
        assertThat(databaseReference, instanceOf(DatabaseReference.class));
        StorageReference storageReference = mainActivity.storageReference;
        assertThat(storageReference, notNullValue());
        assertThat(storageReference, instanceOf(StorageReference.class));
    }
    @Test
    public void onContextPackage() throws Exception
    {
        MainActivity mainActivity = rule.getActivity();
        Context context = rule.getActivity().getApplicationContext();
        assertEquals("edu.cseju.orderpcb", context.getPackageName());
    }