TacapeForte / android-mock

Automatically exported from code.google.com/p/android-mock
0 stars 0 forks source link

Pre-built mocks for newly introduced Android framework classes cannot be used #13

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Scenario: You attempt to use a pre-built mock of an Android framework class 
which is not present in ALL versions of Android (usually because the class has 
been newly introduced in a recent version). 

Problem: When you come to build your test project, android-mock attempts to 
gather all the pre-built mock classes required by your tests based on their 
annotations. For each class it attempts to get a mock class for EVERY version 
of Android supported. If a mock is not available for any of these SDK versions, 
then the process fails for all versions of the mock. 

I believe the offending method is getPrebuiltClassesFor in 
AndroidFrameworkMockGenerator:

  private List<Class<?>> getPrebuiltClassesFor(Class<?> clazz) throws ClassNotFoundException {
    List<Class<?>> classes = new ArrayList<Class<?>>();
    SdkVersion[] versions = SdkVersion.getAllVersions();
    for (SdkVersion sdkVersion : versions) {
      classes.add(Class.forName(FileUtils.getSubclassNameFor(clazz, sdkVersion)));
      classes.add(Class.forName(FileUtils.getInterfaceNameFor(clazz, sdkVersion)));
    }
    return classes;
  }

I've temporarily fixed this locally by making this method more tolerant of the 
class not being found (patch attached). 
What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by t...@leach.it on 1 Jul 2011 at 1:53

Attachments:

GoogleCodeExporter commented 8 years ago
Perhaps it would make more sense to build a series of 
android_framework_mocks.jar files - one for each API level and make the 
developer choose which jar to reference in their test project. They already 
have to make the choice of which Android API level they are developing against 
so this would not seem unreasonable.

Original comment by t...@leach.it on 6 Jul 2011 at 10:48

GoogleCodeExporter commented 8 years ago
I'm seeing this when trying to test NFC Tag classes - I'm seeing the following:
error: Could not find android.nfc.Tag
error: java.lang.ClassNotFoundException: 
v15.genmocks.android.nfc.TagDelegateSubclass

It would be nice if we could specify which version of the API we're targeting 
(like you do in the AndroidManifest.xml and in Eclipse)

Thanks!

Original comment by msprin...@google.com on 29 Jul 2011 at 11:00

GoogleCodeExporter commented 8 years ago

Original comment by sskha...@google.com on 14 Oct 2011 at 5:41

GoogleCodeExporter commented 8 years ago
This issue has been identified and we are introducing an annotation processor 
option that can be used for indicate a list of target versions.

Original comment by sskha...@google.com on 25 Oct 2011 at 11:34

GoogleCodeExporter commented 8 years ago
Added a way to specify target api level as an annotation processor option.

For Gingerbread only, use:
-target_apilevel=10

For Gingerbread and ICS, use:
-target_apilevel=10,14

For all API levels, do not specify the target_apilevel option.

Original comment by sskha...@google.com on 1 Mar 2012 at 9:53