JUnit tests Plain Old Java Objects (POJOs). Basically, it loads your test class, runs a method, instantiates the class that you're testing, tests the methods, does the asserts, and outputs the results. It assumes that all the dependencies are built into the application or .jar file or are on the classpath.
If you want to test a method in a class that subclasses an Android class or depends on an Android class, then you have to test within the Android framework, which is based on JUnit 3. This means that you have to use one of the test classes in android.test.*, and you have to use the instrumented test runner InstrumentationTestRunner (or one of its subclasses). Instructions for doing this are in the Android 2.2 SDK documentation under Testing and Instrumentation. If you run something "inside" the emulator, you're running it in Android, and the assumption is that it's dependent on the Android API. For this reason, you can't test it with test cases built for JUnit 4. However, I have to assume that since these tests are based on JUnit 4, you wrote them prior to starting with Android, and so the classes/ methods they test do not depend on Android (or they didn't at some point in the past). For that reason, if you are *unit* testing these classes/methods, you shouldn't have to use the emulator. You should be able to construct the dependencies in the test fixture, run them with a JUnit 4 test runner, do the asserts, and look at the results. I doubt that this is possible within Android, because Android assumes that you're testing under Android. So you'd have to use something other than the Dalvik VM, which means you can't use the emulator (as far as I know). Seems to me that chances are pretty slim that a test would pass in a non- Dalvik VM and then fail in Android. If the classes you want to test have any dependency on Android classes, then you *have* to test them in Android. By the way, TFJ said that you need to do dummy implementations of Bundle and Intent. What I think he's saying is that you have to do this to run JUnit 4 tests in Android. If you convert your tests to JUnit 3 style, then Android itself provides testing implementations and environments. See the Android 2.2 SDK that I mentioned previously. Elk. On Aug 19, 8:36 am, Eric <[email protected]> wrote: > On Aug 19, 9:57 am, "The.French.DJ" <[email protected]> wrote: > > > It depends on what parts you want to test. If you need the android > > test framework classes for testing activities and services in a valid > > android context then i am not sure if you can make junit 4 work with > > it. > > > However, if you extract your app logic into android independant > > classes then you can test them by having junit 4 in the classpath > > before the android jar. > > Thank you for the info. However, what I don't understand is, will the > JUnit 4 tests be run INSIDE the Android emulator, or outside the > emulator on my Mac in a separate Java VM? Ideally I want the JUnit 4 > tests to be run INSIDE the Android emulator so that I know my code > will be 'ok' when run on an Android device. > > - Eric -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

