0

In the test project for my android app I initially had a series of test for several low level classes. By low level I mean that they are not activities, services or anything specific. They are just classes performing some work according to specs. They relay though on some information from the app context - database, some resources etc.

All tests were green and I was happy. After I added another test class to the lineup to test an Activity all of the sudden my green test started to fail. The Activity tests I added are green but some of the tests which used to be green now throw an exception.

From the dalvik trace it looks like that although my used-to-be green tests do not need anything but the application context, the system still tries to resume some activity (not the one I was trying to unit test with the new tests).

So here is my question: how can I unit test a class which needs just the application context but nothing else? How can I prevent the runtime from trying to launch activities I care nothing about?

2
  • Take a look at Robotium, it works quite well. If you've used Selenium, some aspects should be familiar. Commented Feb 18, 2012 at 2:59
  • @Android He wants to unit test. Robotium is for black box testing, not unit testing. If you really want to unit test Android applications, Robolectric is the only way to go. Commented Feb 20, 2012 at 19:36

2 Answers 2

2

Is sounds like you want to strip out Android from your unit tests. It would be nice if you could use Mocks for this, but unfortunately if you get the Android framework involved, you run into all sorts of problems trying to mock things.

I highly recommend you use Robolectric to "defang" android, and use Mockito to mock stuff you don't care about. This will allow you to continue to run your unit tests (I'm making an assumption here) on the JVM.

Sign up to request clarification or add additional context in comments.

2 Comments

running tests locally would be nice, but this is more than what I can byte off at the moment
It's not as bad as it looks, and IMO is a lot easier than dealing with the Android testing framework once you're all set up. Instructions for setting up Robolectric can be found here if you're using Eclipse. Set up will only take you about 15 minutes. If you're trying to write real unit tests, this is the best way to go about doing so because unit testing is all about isolating your classes, so you want to get Android out of the way, and be able to mock out those classes that are not the current class under test.
0

Hard to say, given what you've communicated so far.

Were you always using InstrumentationTestRunner to run the unit tests? You have to use Instrumentation and InstrumentationTestRunner to test anything that uses Android framework, such as components (Activity, Service, content provider) or Context.

This is documented under Testing in the Developers Guide.

1 Comment

Yes, the test project is configured to run InstrumentationTestRunner (a subclass of thereof).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.