Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests NPE, when I add fragment custom transitions

Tags:

OnBackPressed() test throws NPE when I added a custom transitions. It's fine without the custom transitions. Why?

THIS NPE ONLY HAPPENS IN UNIT TEST, NOT THE APP.

 class MyActivity {
    @Override
    public void onBackPressed() {
        .......
        super.onBackPressed();
    }

    private void putFragment(Fragment fragment) {
            String tag = fragment.getClass().getName();

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // TEST FAILED IF I ADD THE FOLLOWING LINE
            transaction.setCustomAnimations(
                    R.anim.slide_in_left_fade_in,
                    R.anim.fade_out_short_anim_time,
                    R.anim.fade_in_short_anim_time,
                    R.anim.slide_out_right_fade_out);

            transaction.replace(R.id.ub__add_profiles_content, fragment, tag);
            transaction.addToBackStack(tag);
            transaction.commit();
    }
}

class MyActivityTest extends RiderRobolectricBase {

    @Test
    public void onBackPressed() {
       putFragment(someFragment);

       mMyActivity.onBackPressed();
    }
}

Exception:

java.lang.NullPointerException
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
    at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1304)
    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:909)
    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1625)
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:561)
    at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:166)
    at MyActivity.onBackPressed(MyActivity.java:28)