简体   繁体   English

如何使用robolectric测试应用程序类中的方法?

[英]How do I test methods in the application class with robolectric?

I am just getting started with Robolectric and managed to follow the instructions found here: 我刚刚开始使用Robolectric,并设法按照此处的说明进行操作:

http://robolectric.org/eclipse-quick-start http://robolectric.org/eclipse-quick-start

to get the sample to run. 来运行样本。

The first thing I am trying to test are the methods inside my application class but I am getting the following error when I try to run it. 我要测试的第一件事是应用程序类中的方法,但是尝试运行它时出现以下错误。 Which is the line were I do the cast in the code below. 我在下面的代码中进行强制转换的是哪一行。

I guess I am not going about the correct way to get an instance of my application. 我想我不是要获取我的应用程序实例的正确方法。 What is the correct way to get an instance of the application class with Robolectric? 用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)

Here is my test class: 这是我的测试课:

@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));

    }

}

I figured this out. 我想通了。 The sample test used com.example as the package, when I created my test class I put it under a different package (same as my app). 样本测试使用com.example作为包,当我创建测试类时,将其放在另一个包下(与我的应用相同)。 Somehow I lost the setting in the runconfig to point to the real app in the workspace. 我以某种方式丢失了runco​​nfig中的设置,以指向工作空间中的真实应用程序。 After fixing that I now have my tests running. 修复之后,我现在可以运行测试。

The clue was seeing a message in the console about not finding the manifest. 提示是在控制台中看到一条有关找不到清单的消息。

Update 更新

Basically, I was able to fix this by re-tracing my steps thru this section of the above linked guide. 基本上,我可以通过以上链接指南的本节重新跟踪我的步骤来解决此问题。

Create a test Run Configuration Your tests will not run without this step; 创建测试运行配置如果没有此步骤,您的测试将无法运行。 Robolectric will not be able to locate your project resources. Robolectric将无法找到您的项目资源。

Create a test Run Configuration Your tests will not run without this step; 创建测试运行配置如果没有此步骤,您的测试将无法运行。 Robolectric will not be able to locate your project resources. Robolectric将无法找到您的项目资源。

  • Click “Run” → “Run Configurations…” 单击“运行”→“运行配置...”
  • Double click “JUnit” (not “Android JUnit Test”) 双击“ JUnit”(不是“ Android JUnit Test”)
  • Name: MyProjectTests 名称:MyProjectTests
  • Select the “Run all tests in the selected project, package or source folder:” radio button 选择“运行所选项目,程序包或源文件夹中的所有测试:”单选按钮
  • Click the “Search” button 点击“搜索”按钮
  • Select “MyProjectTest” 选择“ MyProjectTest”
  • Test runner: JUnit 4 测试运行器:JUnit 4
  • Click on the link “Multiple launchers available Select one…” at the bottom of the dialog 单击对话框底部的链接“多个发射器可用,选择一个…”。
  • Check the “Use configuration specific settings” box 选中“使用特定于配置的设置”框
  • Select “Eclipse JUnit Launcher” 选择“ Eclipse JUnit启动器”
  • Click “OK” 点击“确定”
  • Click the “Arguments” tab 点击“参数”标签
  • Under “Working directory:” select the “Other:” radio button 在“工作目录:”下,选择“其他:”单选按钮
  • Click “Workspace…” 点击“工作区…”
  • Select “MyProject” (not “MyProjectTest”, The value inside of 'Other' edit box should be '${workspace_loc:MyProject}'), then click “OK” -Click “Apply” then “Close” 选择“ MyProject”(不是“ MyProjectTest”,“ Other”编辑框中的值应为“ $ {workspace_loc:MyProject}”),然后单击“确定”-单击“应用”,然后单击“关闭”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM