Greg
Greg

Reputation: 10788

Can I use Robolectric to unit test static library jars?

I'm trying to test a method in a jar library, and was hoping to use Robolectric to do my unit testing, rather than running the tests in the Android emulator. I'm running into a problem though, where Robolectric needs an androidmanifest.xml file that doesn't exist, since I'm building a library...

Is there any way to run Robolectric tests without an app?

Here's what my test case and code under test look like:

public class ObjectUnderTest {
    methodUnderTest(View v) {
        ...
    }
}

@RunWith(RobolectricTestRunner.class)
public class Tests {
    @Test
    public void methodUnderTest_Test() {
        ...
    }
}

When I run the test suite I get a FileNotFoundException from Robolectric looking for androidmanifest.xml. I've tried using the JUnit4 test runner instead, but then I get the "Stub!" exception when I create a View for the argument to methodUnderTest().

Is there a way to do this besides creating a stub application just for the unit tests? Thanks!

Upvotes: 1

Views: 506

Answers (1)

Eugen Martynov
Eugen Martynov

Reputation: 20130

It depends which Robolectric you're using.

If you use 2.0 you could try to annotate your test class with @Config(manifest=Config.NONE).

If you use 1.x I think it's doable but will require more effort:

  1. You should folder create structure similar to Android project and create dummy AndroidManiifest.xml inside
  2. You should extend RobolectricTestRunner and pass through constructor path to this fake Android project

Upvotes: 1

Related Questions