Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

As you may now, Robolectric is a framework that provides stubs for Android objects, in order to make tests runnable outside the Dalvik environment.

My concern is that, by doing this, one can fake a third party library, which is, I believe, not a good practice (it should be encapsulated instead). If you make assumptions about an interface you don't own, which is changed once your test has been written, you won't be always noticed about the modifications. This can lead to a misunderstanding between your implementations and the interface they depends on.

In addition, Android use mostly inheritance over interfaces which limits contract testing.

So here's my question: Are there situations when Robolectric is the way to go?

Here are some links you can check for further information:

test-doubles-with-mockito

in-brief-contract-tests

share|improve this question

closed as off-topic by gnat, MichaelT, jwenting, Kilian Foth, Snowman Aug 27 at 4:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – gnat, MichaelT, jwenting, Kilian Foth, Snowman
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Can you make your question more specific, especially the title? As it stands, your question seems to be "should I use it? Does it have any merit?" which seems self evident, given that the thing exists. –  Robert Harvey Dec 4 '12 at 16:12
    
You're totally right and I'm sorry. I didn't planed to offend anyone with my question. I'm just looking for practical test cases when Robolectric is a suitable solution. –  Francis Toth Dec 4 '12 at 16:25
    
Not offended, just trying to help you out. –  Robert Harvey Dec 4 '12 at 16:47
3  
Just a guess: perhaps this framework was created back when the arm-dalvik emulation was painfully slow and there was a speed benefit? As it stands it looks like your critique is valid and it's very hard to disagree with it. –  MrFox Dec 4 '12 at 16:54

2 Answers 2

up vote 1 down vote accepted

It seems suitable for projects that have large suites of very CPU-heavy tests that do not depend much on Android specifics. For instance, Roboelectric may care about some logging or similar statements.

For tapping user interfaces and watching for changes, Robotium on the emulator seems better.

However even better design may be to make the CPU-heavy classes completely Android - agnostic. Tests for such classes may run equally well under trivial JUnit under separate testing project.

share|improve this answer

As I understand it, Robolectric rewrites the android.jar on the fly when running the tests. The distributed android package code is edited, to for example supply extra methods to access private internal fields, while leaving the original workings intact. This makes it easy to check if your code it doing what it is supposed to, which is the goal when you're doing Unit tests.

The process is explained in detail by the Robolectric staff here

I use Robolectric daily doing Test Driven Development, and I love it. It does not free me from testing on real devices, and device-specifics still cause some problems, but it does relieve me from a lot of basic programmer bugs caused by mistakes I make myself, plus it helps me keep my code clean and well designed. I rarely have problems that I couldn't have caught with Robolectric.

I think Robolectric is a relevant solution when doing any kind of developing using the principles of TDD on Android, in any type of Android project. It's not super-fast, and pure-Java code is probably tested more quickly in native JUnit, but it is faster than anything else out there when testing Android specific code.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.