如何在
android测试中监听
ActivityTestRule’s
beforeActivityLaunched()方法.
我的解决方法是创建一个自定义的ActivityTestRule并在构造函数上提供回调.这是一种不好的做法吗?同样的方法是可以监听ActivityTestRule构造函数方法.
这是我的代码:
public class CustomActivityTestRule<A extends Activity> extends ActivityTestRule<A> { public interface onBeforeListener{ void onBefore(String message); } private onBeforeListener listener; public CustomActivityTestRule(Class<A> activityClass,onBeforeListener listener) { super(activityClass); } @Override protected void beforeActivityLaunched() { super.beforeActivityLaunched(); listener.onBefore("before activity launch"); } }
在android测试类中,我可以做类似的事情:
@Rule public CustomActivityTestRule<MainActivity> mainActivityActivityTestRule = new CustomActivityTestRule<MainActivity>(MainActivity.class,new CustomActivityTestRule.onBeforeListener() { @Override public void onBefore(String message) { //do something before activity starts } });
同样,它可以在junit规则实例化上执行某些操作.有没有其他方法可以监听junit测试规则实例化?
解决方法
您可以在不创建新类的情况下覆盖beforeActivityLaunched.
我在测试中使用以下内容:
@Rule public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<MainActivity>(MainActivity.class) { @Override protected void beforeActivityLaunched() { super.beforeActivityLaunched(); } };