Hi,
The problem is that you put your class in an android package (android.core). Android Mock assumes that this namespace is reserved for Android platform code and treats it differently. Try putting your classes in a uniquely named package. I expect this will fix it.
Steve
Hi
i have a error from android-mocks.i fellowing is my test codes.
public class AndroidMockTest extends TestCase{
public void testMockInterface(){
ITest test = AndroidMock.createMock(ITest.class);
test.getString((String) AndroidMock.anyObject(),
AndroidMock.anyInt());
AndroidMock.expectLastCall().andReturn("test").times(2);
AndroidMock.replay(test);
String result =test.getString("", 0);
assertEquals( "test",result);
result =test.getString("1", 0);
assertEquals( "test",result);
AndroidMock.verify(test);
}
@UsesMocks(MyClass.class)
public void testPrint() {
MyClass myMockObject = AndroidMock.createMock(MyClass.class);
myMockObject.print(5);
AndroidMock.replay(myMockObject);
myMockObject.print(5);
AndroidMock.verify(myMockObject);
}
}
public class MyClass {
public void print(int arg) {
System.out.println(arg);
}
}
Then,the interface is pass.But the test-class method has throw error.
java.lang.RuntimeException: Could not find mock for
android.core.MyClass -- 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
at
com.google.android.testing.mocking.AndroidMock.getInterfaceFor(AndroidMock.java:
2790)
at
com.google.android.testing.mocking.AndroidMock.createMock(AndroidMock.java:
187)
at
com.google.android.testing.mocking.AndroidMock.createMock(AndroidMock.java:
157)
at
android.project.test.AndroidMockTest.testPrint(AndroidMockTest.java:
32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:
83)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)
Caused by: java.lang.ClassNotFoundException:
genmocks.android.core.MyClassDelegateInterface
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at
com.google.android.testing.mocking.AndroidMock.getInterfaceFor(AndroidMock.java:
2788)
... 22 more
i just refer to the pdf and add ANDROID_FRAMEWORK_MOCKS jar in factroy
path.then when i mock class from android project.it should be throw
this error.
thanks in advance
thanks for you help.
Then i put this class in an other package.but it still throw this
error.
java.lang.RuntimeException: Could not find mock for MyTest.MyClass --
genmocks.MyTest.MyClassDelegateInterface
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at
com.google.android.testing.mocking.AndroidMock.getInterfaceFor(AndroidMock.java:
2788)
... 22 more
On the last part of pdf says: "there should be genmocks folder under
the test project's bin folder." i refer to the pdf,but i don't find
it.
Is that mean that i was setting wrong?
On Nov 20, 6:50 pm, Stephen Woodward <sd.woodwar...@gmail.com> wrote:
> Hi,
>
> The problem is that you put your class in an android package
> (android.core). Android Mock assumes that this namespace is reserved for
> Android platform code and treats it differently. Try putting your classes
> in a uniquely named package. I expect this will fix it.
>
> Steve
Thanks and Regards
Kiba
Hi Kiba
That could be. The problem should be related to something during the compilation phase. I assume you're using eclipse. Check your settings (and the pdf) for the location of your annotation processor log file.
Look in the file for any exceptions or errors. They should indicate what's gone wrong. If the output doesn't make sense please send it along so I can see where things went off track.
Thanks