我刚刚开始使用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));

    }

}

最佳答案

我想通了。
样本测试使用com.example作为包,当我创建测试类时,将其放在其他包下(与我的应用相同)。我以某种方式丢失了runco​​nfig中的设置,以指向工作空间中的真实应用程序。修复之后,我现在可以运行测试。

提示是在控制台中看到一条有关找不到清单的消息。

更新资料

基本上,我可以通过以上链接指南的本部分重新跟踪我的步骤来解决此问题。

创建测试运行配置
没有此步骤,您的测试将无法运行; Robolectric将无法找到您的项目资源。

创建测试运行配置
没有此步骤,您的测试将无法运行; Robolectric将无法找到您的项目资源。


单击“运行”→“运行配置...”
双击“ JUnit”(不是“ Android JUnit Test”)
名称:MyProjectTests
选择“运行所选项目,程序包或源文件夹中的所有测试:”单选按钮
点击“搜索”按钮
选择“ MyProjectTest”
测试运行器:JUnit 4
单击对话框底部的链接“多个发射器可用,选择一个…”。
选中“使用特定于配置的设置”框
选择“ Eclipse JUnit启动器”
点击“确定”
点击“参数”标签
在“工作目录:”下,选择“其他:”单选按钮
点击“工作区…”
选择“ MyProject”(不是“ MyProjectTest”,“其他”编辑框内的值应为“ $ {workspace_loc:MyProject}”),然后单击“确定”
-单击“应用”,然后单击“关闭”

关于android - 如何使用robolectric测试应用程序类中的方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25220138/

10-11 22:21
查看更多