如何正确监视活动 [英] How to properly spy on an Activity

查看:93
本文介绍了如何正确监视活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当窥探到某些东西时,Mockito将创建一个代理实例.现在,是否有任何方法可以将在该代理实例上执行的设置器转发到位于其后的真实实例?

Mockito creates a proxy instance when some thing is spied on. Now, is there any way to forward setters that are then executed on that proxy instance to the real instance that sits behind it?

额定值:我有一个对象实例,但我并没有完全将其控制在自己的控制范围内,即Android活动.我可以为我的应用程序的大部分提供代理版本,并且可以按原样运行,但是,因为我需要在活动创建阶段的很早就创建间谍/代理,但还没有完全完成实例化,例如基本上下文未附加.这发生在代理实例上,并且活动实例本身(通过Activity.this引用自身)当然不使用它.最终结果是,这会导致各种崩溃,因为资源解析是通过此基本上下文发生的,因此内部Fragment机制会抛出NPE等.

Rationale: I have an object instance that I do not have completely under my control, i.e. an Android activity. I can give most parts of my app the proxied version and that runs fine as is, but because I need to create the spy / proxy very early during the creation phase of the activity, it is not yet fully instantiated, e.g. the base context is not attached. This happens on the proxy instance and is of course not used by the activity instance itself (which refers to itself via Activity.this). The end result is that this leads to all kinds of crashes because resource resolving happens via this base context, so the internal Fragment machinery throws NPEs and more.

这是一些代码:

public class CustomAndroidJUnitRunner extends AndroidJUnitRunner {
    @Override
    public Activity newActivity(ClassLoader cl, String className, Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        Activity activity = super.newActivity(cl, className, intent);
        return maybeStubSomeDelegate(activity);
    }

    private Activity maybeStubSomeDelegate(Activity activity) {
        if (!(activity instanceof SomeDelegate)) {
            return activity;
        }
        Activity spiedActivity = spy(activity);
        doReturn(SomeDelegateMock.getInstance())
            .when((SomeDelegate) spiedActivity)
            .getDelegate();
        return spiedActivity;
    }
}

我一无所知-有什么想法吗?

I'm clueless - any ideas?

推荐答案

使用:

android测试支持库的 SingleActivityFactory ActivityTestRule 和Mockito的 spy()

Using:

android Test Support library's SingleActivityFactory, ActivityTestRule and Mockito's spy()

dependencies {
  androidTestImplementation 'com.android.support.test:runner:1.0.2'
  androidTestImplementation 'com.android.support.test:rules:1.0.2'
  androidTestImplementation 'org.mockito:mockito-android:2.21.0'
}

概述:

将间谍实例注入SingleActivityFactory的实现中

Outline:

inject the spied instance inside SingleActivityFactory's implementation

public class MainActivityTest {
    MainActivity subject;

    SingleActivityFactory<MainActivity> activityFactory = new SingleActivityFactory<MainActivity>(MainActivity.class) {
        @Override
        protected MainActivity create(Intent intent) {
            subject = spy(getActivityClassToIntercept());
            return subject;
        }
    };

    @Rule
    public ActivityTestRule<MainActivity> testRule = new ActivityTestRule<>(activityFactory, true, true);

    @Test
    public void activity_isBeingSpied() {
        verify(subject).setContentView(R.layout.activity_main);
    }

}

这篇关于如何正确监视活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆