【发布时间】:2015-01-04 22:29:28
【问题描述】:
我正在尝试学习Robolectric 和Mockito,以实现我正在为已经运行的Android 项目创建测试。但我遇到了一些麻烦。这将使您了解我正在尝试测试的活动:
public class MainActivity extends FragmentActivity {
public Session session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
session = getActiveSession();
if (session == null ) {
// ...
} else {
if (sessionClosed()) {
LoginFragment fragment = new LoginFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragment);
ft.commit();
}
}
}
...
public Session getActiveSession() {
return Session.getActiveSession();
}
public boolean sessionClosed() {
return session.isClosed();
}
}
这段代码的作用是获取活动的Facebook 会话,并根据它的状态显示不同的Fragment。现在我将向您展示测试类:
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
private ActivityController<?> controller;
private MainActivity activity;
private MainActivity spy;
@Before
public void setup() throws Exception {
controller = Robolectric.buildActivity(MainActivity.class);
activity = (MainActivity) controller.create().get();
spy = Mockito.spy(activity);
}
@Test
public void testShowLoginWhenSessionClosed() {
Session session = new Session(Robolectric.application);
Mockito.doReturn(session).when(spy).getActiveSession();
Mockito.doReturn(true).when(spy).sessionClosed();
Mockito.verify(spy).getActiveSession();
Fragment fragment = activity.getSupportFragmentManager()
.findFragmentById(R.id.container);
Assert.assertTrue(fragment instanceof LoginFragment);
}
}
如您所见,我在单独的 java 项目中使用 Robolectric 和 Mockito。当我运行测试类时,我得到以下信息:
Wanted but not invoked:
mainActivity.getActiveSession();
-> at org.example.MainActivityTest.testShowLoginWhenSessionClosed(MainActivityTest.java:101)
Actually, there were zero interactions with this mock.
at org.example.MainActivityTest.testShowLoginWhenSessionClosed(MainActivityTest.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
你能帮我解决这个问题吗?
编辑
我摆脱了 ActivityController,以查看手动处理生命周期是否有效,但我得到了相同的“想要但未调用”消息:
@Test
public void testShowLoginWhenSessionClosed() throws Exception {
Session session = new Session(Robolectric.application);
MainActivity myActivity = Mockito.mock(MainActivity.class);
Mockito.when(myActivity.getActiveSession()).thenReturn(session);
Mockito.when(myActivity.sessionClosed()).thenReturn(true);
myActivity.onCreate(null);
myActivity.getActiveSession(); // works only if i call it manually
Mockito.verify(myActivity).getActiveSession();
Fragment fragment = myActivity.getSupportFragmentManager()
.findFragmentById(R.id.container);
Assert.assertTrue(fragment instanceof LoginFragment);
}
但是这次它说:
However, there were other interactions with this mock:
指向myActivity.onCreate(null) 行。
编辑 2
如果使用 spy,并在 spy Activity 上调用 onCreate(),如下所示:
private ActivityController<MainActivity> controller;
private MainActivity activity;
private MainActivity spy;
@Before
public void setup() throws Exception {
controller = Robolectric.buildActivity(MainActivity.class);
activity = (MainActivity) controller.get();
}
@Test
public void testShowLoginWhenSessionClosed() throws Exception {
Session session = new Session(Robolectric.application);
spy = Mockito.spy(activity);
Mockito.doReturn(session).when(spy).getActiveSession();
Mockito.doReturn(true).when(spy).sessionClosed();
spy.onCreate(null);
Mockito.verify(spy).getActiveSession();
Fragment fragment = spy.getSupportFragmentManager().findFragmentById(R.id.container);
Assert.assertTrue(fragment instanceof LoginFragment);
}
然后我得到以下异常,指向spy.onCreate(null)这一行:
java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4492)
at android.view.LayoutInflater.from(LayoutInflater.java:211)
at org.robolectric.shadows.ShadowActivity.getLayoutInflater(ShadowActivity.java:148)
at android.app.Activity.getLayoutInflater(Activity.java)
at org.example.MainActivityTest.testShowLoginWhenSessionClosed(MainActivityTest.java:109)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
【问题讨论】:
-
这可能是因为您在 通过其生命周期方法驱动非间谍活动之后进行间谍活动...
-
如何监视活动,然后使用 ActivityController 在其生命周期中驱动它?
-
我不确定 - 我找到了 this relevant question,但它没有接受的答案。
标签: java android facebook mockito robolectric