我刚刚开始使用Robolectric并设法按照此处的说明进行操作:
http://robolectric.org/eclipse-quick-start
让样本运行。
我要测试的第一件事是我的应用程序类中的方法,但是当我尝试运行它时,我收到以下错误。我在下面的代码中进行了演员阵容。
我想我不会找到获取应用程序实例的正确方法。 使用Robolectric获取应用程序类实例的正确方法是什么?
java.lang.ClassCastException: android.app.Application cannot be cast to com.gmail.npnster.first_project.MyApp
at com.gmail.npnster.first_project.MyAppTest.setUp(MyAppTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)
这是我的测试类:
@RunWith(RobolectricTestRunner.class)
public class MyAppTest {
private
MyApp app ;
@Before
public void setUp() throws Exception {
// TODO Auto-generated method stub
app = (MyApp) Robolectric.application;
app.clearToken();
app.clearEmailId();
}
@Test
public void testInitalToken() {
assertThat(app.getToken(), equalTo(""));
}
@Test
public void testSaveToken() {
String testToken = "test_token";
app.saveToken(testToken);
assertThat(app.getToken(), equalTo(testToken));
}
}
答案 0 :(得分:2)
我想出来了。 示例测试使用com.example作为包,当我创建我的测试类时,我将它放在不同的包下(与我的应用程序相同)。不知怎的,我在runconfig中丢失了设置,指向工作区中的真实应用程序。修好后,我现在正在运行测试。
线索是在控制台中看到一条关于找不到清单的消息。
基本上,我能够通过上述链接指南的这一部分重新跟踪我的步骤来解决这个问题。
创建测试运行配置 没有这一步,您的测试将无法运行; Robolectric无法找到您的项目资源。
创建测试运行配置 没有这一步,您的测试将无法运行; Robolectric无法找到您的项目资源。