aspose file tools*
The moose likes Android and the fly likes Unit Testing classes that aren't Activity/Service etc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Unit Testing classes that aren Watch "Unit Testing classes that aren New topic
Author

Unit Testing classes that aren't Activity/Service etc

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

If you want to test any class that imports/uses an Android class, you have to run that test in the emulator, and Android has a nice Test harness to test your Activities and such in an Android App.

But how do you test classes that use Android class, but isn't an Android App, but instead a Library/Framework to use in an Android App. The Library/Framework is a standalone project, but it uses Android classes. How do I write Unit Tests for those classes I can't extend the standard Android TestCase classes because my classes aren't Activities, Services, or any of that.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Marko Gargenta
author
Greenhorn

Joined: Mar 27, 2011
Posts: 18

Android Unit Testing framework does cover all Android components, so Activities, Services, Broadcast Receivers, and Content Providers. See: http://d.android.com/reference/android/test/package-summary.html for more details.

For other Java classes, keep in mind that Android JUnit is built on top of regular JUnit, so you can use regular framework for testing.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Marko Gargenta wrote:Android Unit Testing framework does cover all Android components, so Activities, Services, Broadcast Receivers, and Content Providers. See: http://d.android.com/reference/android/test/package-summary.html for more details.

For other Java classes, keep in mind that Android JUnit is built on top of regular JUnit, so you can use regular framework for testing.


These are none of those four. I guess I didn't describe it well. These are plain classes, but you cannot use regular framework for testing because the classes I am testing does import Android classes in them. I am building a library/tool developers can use when developing their applications. Since they import Android classes and use them, then regular JUnit framework would throw an error because the android.jar file in the classpath just has stubs in them, no real code.

Mark
Marko Gargenta
author
Greenhorn

Joined: Mar 27, 2011
Posts: 18

I see. Hmm, so you have Java code that imports android.* and you want to test it but using JUnit.

You could subclass TestCase from regular JUnit package:
http://developer.android.com/reference/junit/framework/package-summary.html
and run it that way. You'd still run it within an Android app, so you'd need to have some kind of Activity or something to call your library.

Hope this helps.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Marko Gargenta wrote:I see. Hmm, so you have Java code that imports android.* and you want to test it but using JUnit.

You could subclass TestCase from regular JUnit package:
http://developer.android.com/reference/junit/framework/package-summary.html
and run it that way. You'd still run it within an Android app, so you'd need to have some kind of Activity or something to call your library.

Hope this helps.


Yeah, but my app isn't an android app, but a library of classes you can use on Android to simplify SQLite database access. And I need to write tests for that.

If you subclass TestCase you will always get a Stub exception because the android.jar file only has stubs, no actual implementation and code. That code is only provided on the emulator or a real device. And creating an Activity without needing any UI just so you can test some code seems so Cludgy to me. But Android overall, is a little bit cludgy anyway. (opinion of course)

Mark
Marko Gargenta
author
Greenhorn

Joined: Mar 27, 2011
Posts: 18

Well if you are writing code that depends on android.* code, you must run it on an Android device. That is because android.* classes depend on rest of the stack on the system. And to run it, it should be an Android application. So, one option is to create an application that is driving your library on the device. Other option is not to use android.* code and make your library work off of standard SQLite classes in witch case you can test outside Android.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5778
    
    7

I have found that I have to very carefully separate the "controller" and "view" code (which depends on the Android Framework) from the "model" code (which doesn't). I can then write standard JUnit unit tests against the model classes, and have them running as part of the test phase of a Maven build. In order to support this I have had to abstract a few things:

* I have an interface that will yield a file. For the code running on Android I have a class that yields a file on the sdcard. For the unit test I have a class that yields a file in the target directory.
* I have had to also factor out the logging so that on Android I use the Android logger and in the unit tests I use commons logging with Log4J (my next experiment will be if i can use commons logging on Android itself which should make logging easier).

I'm sure I will find more issues as I got further one. Essentially, anyplace in my model classes where I think I might need to use an Android class I will have to create an interface instead and supply an alternate implementation for the unit tests.


JBoss In Action
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Thanks guys.

Yeah, I will have to run the tests on the emulator. The library is integral to Android. It isn't just an abstraction above SQLLite, but SQLLite and Android.

I have found a video that might help me.
http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unit Testing classes that aren't Activity/Service etc
 
Similar Threads
Is there any Unit testing framework for Andriod Code ?
Android testing, what *should* I be testing?
How can I test a android application which is based on event notification in Juni?
Testing simulation
How to use the Android Compatibility Package?