nPn
nPn

Reputation: 16738

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:

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?

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

    }

}

Upvotes: 2

Views: 1630

Answers (1)

nPn
nPn

Reputation: 16738

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). Somehow I lost the setting in the runconfig to point to the real app in the workspace. 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.

Create a test Run Configuration Your tests will not run without this step; Robolectric will not be able to locate your project resources.

  • Click “Run” → “Run Configurations…”
  • Double click “JUnit” (not “Android JUnit Test”)
  • Name: MyProjectTests
  • Select the “Run all tests in the selected project, package or source folder:” radio button
  • Click the “Search” button
  • Select “MyProjectTest”
  • Test runner: 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”
  • 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”

Upvotes: 3

Related Questions