单元测试查询
我必须测试通过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
中完成的异步工作,因此它具有自己的测试,但是我需要测试接口的方法是否正确执行以及它们是否触发了预期的结果(例如通过post
等正确的EventBus
事件)。
我一直在尝试找到一种方法来获取测试类中的这些实例化接口,但似乎找不到解决方法。
我曾尝试使用PowerMockito
来获取protected
方法,看看是否可以返回interface
,但没有成功。
此外,此接口还包括要模拟的某些特定api
方法的实例化。关于如何进行以及是否可以进行的任何线索?我知道架构听起来并不理想。我该如何进行?
我知道了,但是必须先转换为Kotlin。
[我使用Kotlin的委托,所以CustomerInitialiserImpl
将实现SpAsyncHandler
的接口,该接口通常会注入普通的SpAsyncHandlerImpl
,但是在单元测试中,我会通过MockSpAsyncHandlerImpl
设置委托, ,除其他外,只需在主线程上执行所有接口函数即可。传递模拟对象时,它就像是一种魅力!