Skip to content Skip to sidebar Skip to footer

Testing With Robolectric In Eclipse

I am trying to run a simple test with Robolectric in eclipse. I followed this tutorial, but got this error : java.lang.RuntimeException: java.io.FileNotFoundException: C:\Vishal\

Solution 1:

Vishal,

you're mixing test and runner classes.

junit works in general like:

for (TestClass c: allTests) {
    Testt= TestClass.newInstance();

    testListener.startTest(t.getName());
    try {
       t.beforeTest();
       t.run();
       t.afterTest();

       testListener.testPass(t.getName());
    } catch (Exception e) {
       if (t.isExpectedException(e)) {
           testListener.testPass(t.getName());
       } elseif (isAssertException(e)) {
           testListener.testFail(t.getName(), getExplanation(e));
       } else {
           testListener.testError(t.getName(), e);
       }
    }

    testListener.testEnd(t.getName());
}

Because of Class.newInstance() method call it's required to have public non-zero parameters constructor for test class. Annotations @Test, @Before and @After mark to junit which classes are test classes.

The code above is kind of test runner class. Sometimes you need additional logic from test runner class. For example Robolectric and PowerMock are replacing standard ClassLoader with own specific for purposes. Robolectric to provide own implementation for Android classes. PowerMock for ability to mock static, final classes and methods, as well for mocking classes constructors.

@RunWith annotation exists to notify junit that it should use another test runner for test class. So Robolectric Android test should have @RunWith(RobolectricTestRunner.class). However you have ability to add additional functionality to Robolectric test runner by extending RobolectricTestRunner class. For example for own shadowing or pointing to specific AndroidManifest.xml file.

So you should have your own test runner class AHTesRunner where you will point Robolectric to specific manifest file location:

publicclassAHTestRunnerextendsRobolectricTestRunner {}

And you need to notify junit that it should use your own test runner (customized Robolectric test runner):

@RunWith(AHTestRunner.class)
public class AHTest {
    @Before
    ......
    @Test
    ......
    @After
}

Hope this is much clear now

Post a Comment for "Testing With Robolectric In Eclipse"