Sie sind auf Seite 1von 7

Setting Up Android Mock in Eclipse

Initial Steps
To begin with, set up your Android project and test project in Eclipse as normal. Download AndroidMockGenerator.jar and AndroidMockRuntime.jar from http:// code.google.com/p/android-mock. Add AndroidMockGenerator.jar as a Referenced Library in your Android test project.

Adding and Configuring the Annotation Processor


1. Right click on your test project and select Properties. 2. Choose Java Compiler and turn on Enable project specific settings. Ensure that the Compiler compliance level is set to 1.6.

3. 4. 5. 6.

Choose Annotation Processing and turn on Enable project specific settings. Turn on Enable annotation processing. Turn off Enable processing in editor. Add a new Processor option (click New...) with key bin_dir and set the value to the absolute path to your test project's bin folder (the folder set as the project's output folder). 7. Add another Processor option with key logfile and set the value to any location where you would like to log debug output from the Annotation processor. A good location for this is a file named AnnotationProcessor.log in your test project's base folder. This option is not required, but is highly recommended, as Eclipse suppresses all other types of output from the Annotation Processor, thus this file will be your only source of information.

8. Choose Factory Path 9. Select Add External JARs... 10. Navigate to and Add AndroidMockGenerator.jar (this is shown below as Path to AndroidMockGenerator.jar)

11. Add the required class path entries shown above by: a. Select Add Variable...

b. Click Configure Variables...

c. New... d. There are 2 required entries (which are shown below). These are the path to the android.jar file provided by the Android SDK, and the path to the output folder (usually named bin) of your main project (not the test project). e. For each desired entry, you need to choose a name (your choice) and a path: i. Name: (choose a name youll understand), Path: (absolute path to the desired jar file or output folder). ii. Add a path entry for the Android Runtime (android.jar) by choosing New... and then File.... This should be the same jar file as the one Eclipse automatically includes in your Android Project. E.g. (as below) 1. Name: ANDROID_RUNTIME 2. Value: /usr/local/android-sdk/android.jar iii. Add a path entry for the Android project's bin folder (the project being tested, not the test project) by choosing New... and then Folder... E.g.

(as below) 1. Name: APP_UNDER_TEST 2. Value: /usr/local/workspace/my_app/bin iv. Add additional path entries for any Jars in your Referenced Libraries which contain classes that your tests wish to Mock following the examples above.

Checking that it all worked


The ultimate success test is to run your tests (using mocks) and see if they pass. Other places to look for information about the mock generation are: * Check under the test project's bin folder for a folder called genmocks. If you mock java.lang.Object (as a trivial example) a folder called genmocks/java/lang/ will be generated containing two classes which represent the mocks of java.lang.Object. * Check the output of the annotation processing in the log file which you specified by the Processor option in the instructions above (named logfile). This contains all of the output generated by a run of the Annotation Processor including any stack traces. Each execution will start with a NOTE : Start Processing Annotations message and end with NOTE : Finished Processing Mocks. A single build may include multiple runs of the annotation processor.

Das könnte Ihnen auch gefallen