问题描述
我试图来测试的Mockito和功放的活动; 匕首。我已经能够在我的应用程序中注入依赖关系的活动,但测试活动的时候,我一直没能模拟的活动注入。我应该注入活性测试或让getActivity()创建它吗?
公共类MainActivityTest扩展
ActivityInstrumentationTestCase2< MainActivity> {
@注入发动机engineMock;
私人MainActivity mActivity;
私人按钮mLogoutBtn;
公共MainActivityTest(){
超(MainActivity.class);
}
@覆盖
保护无效设置()抛出异常{
super.setUp();
//注入engineMock测试
。ObjectGraph.create(新TestModule())注资(本);
}
@覆盖
保护无效的teardown(){
如果(mActivity!= NULL)
mActivity.finish();
}
@Module(
包括= MainModule.class,
入口点= MainActivityTest.class,
覆盖=真
)
静态类TestModule {
@Provides
@Singleton
发动机provideEngine(){
返回模拟(Engine.class);
}
}
@UiThreadTest
公共无效testLogoutButton(){
当(engineMock.isLoggedIn())thenReturn(真)。
mActivity = getActivity();
mLogoutBtn =(按钮)mActivity.findViewById(R.id.logoutButton);
//如何在测试注入engineMock到活动?
ObjectGraph.create(新TestModule())注入(this.mActivity)。
assertTrue(mLogoutBtn.isEnabled()==真);
}
}
我用的Mockito和匕首进行功能测试。关键的概念是,你的测试类从ActivityUnitTestCase,而不是ActivityInstrumentationTestCase2继承;后者的超一流呼叫ONSTART()活动的生命周期方法阻止你注入测试双打的依赖关系,但与第一次超一流的,你可以处理生命周期更精细。
您可以看到在使用匕首1.0.0和的Mockito测试活动和片段我的工作的例子:
https://github.com/IIIRepublica/android-civicrm-test
在测试的项目是:
https://github.com/IIIRepublica/android-civicrm
希望这有助于你
I’m trying to test an Activity with Mockito & Dagger. I have been able to inject dependencies to Activity in my application but when testing the Activity, I have not been able to inject mock to the Activity. Should I inject Activity to test or let getActivity() create it?
public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
@Inject Engine engineMock;
private MainActivity mActivity;
private Button mLogoutBtn;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
// Inject engineMock to test
ObjectGraph.create(new TestModule()).inject(this);
}
@Override
protected void tearDown() {
if (mActivity != null)
mActivity.finish();
}
@Module(
includes = MainModule.class,
entryPoints = MainActivityTest.class,
overrides = true
)
static class TestModule {
@Provides
@Singleton
Engine provideEngine() {
return mock(Engine.class);
}
}
@UiThreadTest
public void testLogoutButton() {
when(engineMock.isLoggedIn()).thenReturn(true);
mActivity = getActivity();
mLogoutBtn = (Button) mActivity.findViewById(R.id.logoutButton);
// how to inject engineMock to Activity under test?
ObjectGraph.create(new TestModule()).inject(this.mActivity);
assertTrue(mLogoutBtn.isEnabled() == true);
}
}
I use Mockito and Dagger for functional testing.The key concept is that your test class inherits from ActivityUnitTestCase, instead of ActivityInstrumentationTestCase2; the latter super-class call onStart() life-cycle method of Activity blocking you for inject your test doubles dependencies, but with first super-class you can handle the life-cycle more fine-grained.
You can see my working examples using dagger-1.0.0 and mockito for test Activities and Fragments in:
https://github.com/IIIRepublica/android-civicrm-test
The project under test is in:
https://github.com/IIIRepublica/android-civicrm
Hope this helps you
这篇关于Android的功能测试与匕首的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!