SDKVersion.UNKNOWN with regenerateframemocks=true

12 views
Skip to first unread message

Sundeep Khandpur

unread,
Sep 29, 2011, 3:34:18 PM9/29/11
to Android Mock Discussion, rob...@google.com, iv...@google.com
Hi,

I am using the flag regenerateframemocks=true and it generates the
mocks with UNKNOWN sdk version (i.e. "" as the prefix of the head
directory) but when I run the tests, it looks for v233...I looked at
the code and basically it does the following:

For generating the mocks, it calls:
createMocksForClass(clazz, SdkVersion.UNKNOWN);

for loading the mocks during runtime, it checks calls getInterfaceFor:
private static <T> Class<T> getInterfaceFor(Class<T> clazz) {
try {
String className;
if (isAndroidClass(clazz)) {
className = FileUtils.getInterfaceNameFor(clazz,
SdkVersion.getCurrentVersion());
} else {
className = FileUtils.getInterfaceNameFor(clazz,
SdkVersion.UNKNOWN);
}
return (Class<T>) Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Could not find mock for " +
clazz.getName()
+ " -- Make sure to run the MockGenerator.jar on your test
jar, and to "
+ "build the Android test APK using the modified jar created
by MockGenerator", e);
}
}

static boolean isAndroidClass(Class<?> clazz) {
String packageName = clazz.getPackage().getName();
return packageName.startsWith("android.") ||
packageName.startsWith("dalvik.")
|| packageName.startsWith("java.") ||
packageName.startsWith("javax.")
|| packageName.startsWith("org.xml.sax") ||
packageName.startsWith("org.xmlpull.v1")
|| packageName.startsWith("org.w3c.dom") ||
packageName.startsWith("org.apache.http")
|| packageName.startsWith("junit.");
}

------------------
Since the isAndroidClass return true, it checks for the mocks under
v233....which does not exist.

What is the best thing I can do if I do not want to include
android_framework_mocks.jar.

Some alternatives/workaround I considered:
a) Make changes to the code of AndroidMockGenerator/AndroidMockRuntime
to be consistent with the generation and loading SDK Versions
b) Before dexing, create a folder v233 and put genmocks folder inside
it, so the proper version name is present.

Any ideas what else can be done?

Robert Tsai (蔡力元)

unread,
Sep 29, 2011, 3:45:52 PM9/29/11
to Sundeep Khandpur, Android Mock Discussion, iv...@google.com
The bug is that the Android classes should be going under v233/genmocks/..., not genmocks/...

You have to figure out why the annotation processor/mockgenerator is outputting things to genmocks/ and not v233/genmocks/

--Rob

Sundeep Khandpur

unread,
Sep 29, 2011, 4:16:14 PM9/29/11
to Robert Tsai (蔡力元), Android Mock Discussion, iv...@google.com
Yes that is because I specify regenerateframemocks=true:
--
  if (!AndroidMock.isAndroidClass(clazz) || regenerateFrameworkMocks) {
          mockedClassesSet.addAll(getAndroidMockGenerator().createMocksForClass(clazz));
        }

And what createMocksForClass(clazz) does is:
public List<GeneratedClassFile> createMocksForClass(Class<?> clazz)
      throws ClassNotFoundException, IOException, CannotCompileException {
    return this.createMocksForClass(clazz, SdkVersion.UNKNOWN);
  }
---
--
Thanks.
--Sundeep

Stephen Woodward

unread,
Sep 29, 2011, 4:38:30 PM9/29/11
to androi...@googlegroups.com

The point of regeneratemocks is to avoid using the framework mocks jar. Try without that option and see how it works.

On Sep 29, 2011 9:16 PM, "Sundeep Khandpur" <sskh...@google.com> wrote:
> Yes that is because I specify regenerateframemocks=true:
> --
> if (!AndroidMock.isAndroidClass(clazz) || *regenerateFrameworkMocks*) {

>
> mockedClassesSet.addAll(getAndroidMockGenerator().createMocksForClass(clazz));
> }
>
> And what createMocksForClass(clazz) does is:
> public List<GeneratedClassFile> createMocksForClass(Class<?> clazz)
> throws ClassNotFoundException, IOException, CannotCompileException {
> return this.createMocksForClass(clazz, *SdkVersion.UNKNOWN*);
Reply all
Reply to author
Forward
0 new messages