简体   繁体   English

如何使凌空onResponse在UnitTest线程中运行

[英]how to make volley onResponse run in UnitTest thread

i'm making unit test for my application my unit test class has this method 我正在为我的应用程序进行单元测试,我的单元测试类具有此方法

@Before
public void initialize() {
    mContext = InstrumentationRegistry.getTargetContext();
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(mContext).name("realmTest").inMemory().build();
    Realm.setDefaultConfiguration(realmConfiguration);
    mWorkoutsModel = new WorkoutsModel(mContext);
    mRealm = Realm.getInstance(realmConfiguration);
    mWorkoutsModel.registerListener(this);
}

@Test
public void getWorkouts() throws Exception {
    mWorkoutsModel.onStart();

    mLock.await();
    mWorkoutsModel.onStop();

}

@After
public void deInitialize() {
    mWorkoutsModel.unRegisterListener();
    mRealm.close();
}

and my model 和我的模特

@Override
public void onStart() {

    mRealm = Realm.getDefaultInstance();
        getDataFromApi();
}

private boolean getDataFromApi() {
    Constants.AllAPIs.ALLWorkouts allWorkouts = new Constants.AllAPIs.ALLWorkouts();
    if (Permissions.isInternetConnectionExist(mContext)) {
        mApiHandler.downLoadDataFromApi(AllWorkouts.class, allWorkouts.getBaseUrl(),
                new APIHandler.StringResponseHandler<AllWorkouts>() {
                    @Override
                    public void onResponse(AllWorkouts response) {
                            insertWorkouts(response.getWorkouts());
                },
                new APIHandler.ErrorResponseHandler() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }, TAG);

        return true;
    } else {
        return false;
    }
}
private void insertWorkouts(final List<Workout> workouts) {
    mCurrentInsertTransaction = mRealm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            bgRealm.copyToRealmOrUpdate(workouts);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
        }
    });
}

my problem that the unittest calls onStart which create realm object in the model in test thread but volley force onResponse to run on UIThread which makes realm throw exception Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created. 我的问题是unittest调用onStart,该onStart在测试线程中的模型中创建模型中的领域对象,但是凌空强制onResponse在UIThread上运行,这会使领域抛出异常,从而导致Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created. Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

the code runs perfect in normal, but in test it fails 该代码在正常情况下运行完美,但在测试中却失败了

does anyone faced same problem or can solve it ? 有没有人遇到同样的问题或可以解决?

i solved my problem by run the test in handler 我通过在处理程序中运行测试解决了我的问题

new Handler(mContext.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            try {
                mWorkoutsModel.onStart();
                mLock.await();
                mWorkoutsModel.onStop();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

hope that help somebody 希望对某人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM