[英]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. 我以某种方式丢失了runconfig中的设置,以指向工作空间中的真实应用程序。 After fixing that I now have my tests running. 修复之后,我现在可以运行测试。
The clue was seeing a message in the console about not finding the manifest. 提示是在控制台中看到一条有关找不到清单的消息。
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将无法找到您的项目资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.