单元测试查询
我必须测试几个类的功能,通过它们的superclass
来处理。
即:我们有一个自定义类CustomerInitialiserImpl
,它扩展了SpAsyncHandler
,后者有以下方法:protected executeAsync(ExecuteAsync executeAsync)
(参数ExecuteAsync
是一个接口,而方法executeAsync
所做的是在一个或多个异步线程中以特定顺序运行接口方法)。
ExecuteAsync
看起来如下所示:
public interface ExecuteAsync {
void initApi();
void doInBackground();
void onPostExecute();
}
CustomerInitialiserImpl
有以下方法:
public void initialiseCustomer(int id) {
executeAsync(new ExeuteAsync() {
/*here are instantiated the 3 overridden methods of the interface*/
});
}
由于我不关心在SpAsyncHandler
中完成的异步工作,所以它有自己的测试,但是,我需要测试接口的方法是否正确地执行,以及它们是否触发预期的结果(f.i )。通过post
等进行正确的EventBus
事件。
我一直试图找到一种方法来获取我的测试类上的那些实例化接口,但是我似乎找不到绕过它的方法。
我尝试过使用PowerMockito
获取protected
方法,并查看是否可以返回interface
,但没有成功。
此外,该接口还包括要模拟的一些特定api
方法的实例化。有什么线索可以说明我该怎么做,如果可以的话?我知道这个建筑听起来不太理想。我该怎么做?
发布于 2020-01-16 07:17:23
我已经想明白了,但我得先皈依科特林。
我使用了Kotlin的委托,这样CustomerInitialiserImpl
将实现SpAsyncHandler
的接口,这通常会注入正常的SpAsyncHandlerImpl
,但是在Unit中,我将通过MockSpAsyncHandlerImpl
设置委托,这除其他外,只需在主线程上执行所有接口函数。传递被模仿的对象,它的工作就像一个魅力!
https://stackoverflow.com/questions/57312186
复制相似问题