I have never tried to use vanilla JUnit 4 running under Android. I am
not absolutely certain, but I predict that it won't work. To run any
test, you need to use InstrumentationTestRunner, and it assumes JUnit
3, which is substantially different from JUnit 4. However, the
documentation for JUnit 4 says:
You make your JUnit 4 test classes accessible to a TestRunner designed
to work with earlier versions of JUnit, declare a static method suite
that returns a test.
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(Example.class);
}
I'd look at the Javadoc for junit.framework.*. When I Googled it, I
saw that it included JUnit4TestAdapter. Better yet, search for help on
running JUnit 4 tests under JUnit 3. InstrumentationTestRunner is
designed to work with earlier versions of JUnit, so anything that can
convert a JUnit 4 suite to JUnit 3 should work in Android. That the
documentation for JUnit 4 includes instructions for running JUnit 4
suites under an earlier test runner implies that it's a known
situation, so I assume somebody else has written about it. Seems like
nobody else writing for Android has encountered it (or bothered to
reply), but at least for "vanilla" tests someone probably has done
something.
Sorry I can't help you any more.
PS - I notice that the main differences between JUnit 3 and JUnit 4
tests are a) JUnit 3 denotes a test suite by subclassing
junit.framework.TestCase (JUnit 4 has no such requirement) b) JUnit 3
test methods are denoted by the string "test" as the prefix of the
method name, while JUnit 4 uses the @Test annotation. It doesn't seem
to me that there are any other substantial differences, but as I have
already said, I am not a JUnit 4 expert.
On Aug 19, 3:33 pm, Eric <[email protected]> wrote:
> I absolutely want my POJO JUnit 4 tests running on the Android
> emulator VM, even if they do not reference Android APIs. The reason
> for this is fairly clear. Android has a different implementation of
> the base Java APIs, and it uses a special VM, so I want to know that
> all of the shared code I will take advantage of beneath the Android UI
> layer will run without fail on the emulator.
>
> I hate to say this, but I am _still_ confused as to whether or not
> this is possible. Your eloquent answer didn't seem to answer the
> question specifically. Quite simply, is it even possible to run POJO
> JUnit 4 test suites on the Android device itself, and if so, what is
> the best/quickest way to achieve this from a configuration standpoint
> in Eclipse?
>
> On Aug 19, 4:27 pm, "A. Elk" <[email protected]> wrote:
>
> > 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