Android单元测试是片段nullpointexception

时间:2017-08-10 09:49:20

标签: android unit-testing android-fragments

PersonListActivity触发器BaseFragmentActivity正在运行时,我正在BaseFragmentActivity中按onCreate - >>的顺序执行这些过程doCreate - > initFragment。在fragment = createFragment();内部调用initFragment时,同一时间createFragment()函数正在PersonListActivity内部工作,然后调用PersonListFragment ..

问题是,当我使用单元测试环境(com.hpolat.example.personapp。test。...)时,PersonListFragment始终响应为nullpoinexception 。我注意到PersonListFragment在调试期间为空。我如何阻止PersonListFragment不响应null。

感谢您的建议!

应用代码;

PersonListActivity.class

    public class PersonListActivity
            extends BaseFragmentActivity
    {
        @Override
        public Fragment createFragment() {
            return PersonListFragment.newInstance();
        }
    }

BaseFragmentActivity.class

public abstract class BaseFragmentActivity
        extends AppCompatActivity
{
    private static final String FRG_ACTIVITY = "ActivityFragmentName";
    private Fragment mFragment;

    protected abstract Fragment createFragment();

    @LayoutRes
    protected int getLayoutResId() {
        return R.layout.layout_fragment_container;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.doCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    protected void doCreate(Bundle savedInstanceState) {
        setContentView(getLayoutResId());
        if (savedInstanceState != null) {
            this.restoreState(savedInstanceState);
        }

        setExtraLayoutItems();

        initFragment();
    }

    public void initFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(getFragmentContainerId());

        if (fragment == null) {
            fragment = createFragment();

            if (fragment != null) {
                fragmentManager.beginTransaction()
                               .add(getFragmentContainerId(), fragment)
                               .commit();
            }
        }
        mFragment = fragment;
    }

PersonListFragment.class

public class PersonListFragment
        extends BaseListFragment
{
    public static final int LOADER_PERSON_ID = 2;

    public PersonListFragment() {
        //nop
    }

    public static PersonListFragment newInstance() {
        return new PersonListFragment();
    }

    @NonNull
    @Override
    public Integer getListLoaderId() {
        return LOADER_PERSON_ID_ID;
    }

单元测试代码块;

public class PersonListFragmentTest{

    @Mock
    private PersonListFragment mPersonListFragment;
    private PersonListActivity mPersonListActivity;

    @Mock
    private BaseFragmentActivity mBaseFragmentActivity;


    @Before
    public void setup() throws Exception {
        mPersonListActivity = new PersonListActivity();
        mPersonListFragment = new PersonListFragment();

        mBaseFragmentActivity = new BaseFragmentActivity() {
            @Override
            protected Fragment createFragment() {
                return PersonListFragment.newInstance();
            }
        };

        mBaseFragmentActivity.initFragment(); //PersonListFragment is null

    }

    @Test
    public void getListLoaderIdTest_True() {
        assertEquals(1, 1); // for setup

    }

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。该片段无法正常工作,因为我在测试文件夹中工作。当我在androidTest文件夹中重新编写测试时,我得到了正确的结果而没有错误。

(com.hpolat.example.personapp。androidTest ....),