简体   繁体   中英

Unit testing non-Android specific code in an Android app without emulator

Android newbie here, using ADT

I have a few number crunching classes in my app that are "pure" Java - they don't depend on anything in the Android SDK. Ideally, I'd like to test them without firing off the (somewhat slow) emulator. Is that possible and if yes, how?

From what I gather so far, Dalvik compilation and resulting classes are different than the ones expected from "normal" JVMs and that rules out regular JUnit tests ( what happens if you try ). Using the JUnit in the Android SDK starts the emulator. Here's where my Google fu fails me for everyone is preoccupied testing UI.

Is that possible and if yes, how?

Option #1: Follow Mike B's comment and move that code to a separate JAR project with its own unit tests.

Option #2: Use Robolectric to run dedicated tests for these classes on the JVM. While Robolectric is mostly designed to test Android-y code on the JVM, I see no reason why you could not use it for your scenario as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM