dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

[Android]: NoClassDefFoundError when mocking throwing an exception #386

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Add mockito, dexmaker and dexmaker-mockito to project
2. Create a mock for SomeClass (code provided in additional information)
3. Instruct to throw SomeException when calling doSomething() method
4. Call doSomething() method

What is the expected output? What do you see instead?
Expected:
Exception is to be thrown

Instead:
Exception is thrown:
java.lang.NoClassDefFoundError: sun.reflect.ReflectionFactory
at 
org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.<init>(SunReflec
tionFactoryInstantiator.java:24)
at 
org.objenesis.strategy.StdInstantiatorStrategy.newInstantiatorOf(StdInstantiator
Strategy.java:65)
at org.objenesis.ObjenesisBase.getInstantiatorOf(ObjenesisBase.java:76)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
at org.objenesis.ObjenesisHelper.newInstance(ObjenesisHelper.java:28)
at 
org.mockito.internal.stubbing.answers.ThrowsExceptionClass.answer(ThrowsExceptio
nClass.java:27)
at 
org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationM
atcher.java:34)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:91)
at 
org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:2
9)
at 
org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifier
Handler.java:38)
at 
com.google.dexmaker.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAda
pter.java:54)
at SomeClass_Proxy.doSomething(SomeClass_Proxy.generated)
at 
com.example.SomeTestCase.testDoSomethingSomeExceptionThrown(SomeTestCase.java:10
8)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at 
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:53
7)
at 
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)

What version of the product are you using? On what operating system?
Mockito 1.9.5 (mockito-all version)
dexmaker 0.9
Android 4.0.1 (emulator)
JUnit3

Please provide any additional information below.
0. You guys did great job by providing Mockito to Android developers. I love it!
1. Mocking value returns and verification of methods running appears to be 
working fine.
2. Some code:
public class SomeClass {
    public void doSomething() throws SomeException {

    }

}

public class SomeException extends Exception {

    /**
     * 
     */
    private static final long serialVersionUID = 1544471714051523296L;

}

public class SomeTestCase extends TestCase {
    public void testSendAsyncMessageMessageNotSentException() {
        SomeClass someClass = Mockito.mock(SomeClass.class);

        try {
            Mockito.doThrow(SomeException.class).when(someClass).doSomething();
            someClass.doSomething();
            fail();
        } catch (SomeException e1) {
            //success
        } catch (Exception e) {
            fail();
        }
    }
}

Original issue reported on code.google.com by tomasz.p...@gmail.com on 23 Oct 2012 at 7:03

GoogleCodeExporter commented 8 years ago
Hi, mmmh so Objenesis doesn't always work on Dalvik, that should have been 
expected.

I think a workaround would be to pass an exception instance or to use an answer 
that creates an exception.

Original comment by brice.du...@gmail.com on 23 Oct 2012 at 8:20

GoogleCodeExporter commented 8 years ago
The workaround suits me perfectly.

As it comes to what I experienced, it appears that ReflectionFactory is not 
available on Dalvik. Perhaps some more explanatory information should be passed 
in here.

Nevertheless, this submission should help if anyone comes across same issue.

Original comment by tomasz.p...@gmail.com on 23 Oct 2012 at 9:18

GoogleCodeExporter commented 8 years ago
This is expected from sun.* packages. Objenesis is using JVM specific/internal 
implementation.
You should also report an issue on the dexmaker project.

Original comment by brice.du...@gmail.com on 23 Oct 2012 at 9:40

GoogleCodeExporter commented 8 years ago
There it is:
http://code.google.com/p/dexmaker/issues/detail?id=17

Original comment by tomasz.p...@gmail.com on 23 Oct 2012 at 10:47

GoogleCodeExporter commented 8 years ago
Cool, thanks for reporting :)

Original comment by brice.du...@gmail.com on 23 Oct 2012 at 11:16

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 19 Dec 2012 at 10:59

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 8 Jan 2013 at 4:50

GoogleCodeExporter commented 8 years ago
Objenesis 1.3 will probably have some code that will work on Dalvik. Progress 
is ongoing, thanks to Henri Tremblay and Ian Parkinson, maybe others as well :

https://groups.google.com/forum/?fromgroups=#!topic/objenesis-dev/I8J3fGGZvUQ

Original comment by brice.du...@gmail.com on 8 Jan 2013 at 4:55

GoogleCodeExporter commented 8 years ago
@tomasz Objenesis 1.3 is out could you check if that's working ?

Original comment by brice.du...@gmail.com on 28 Jan 2013 at 11:40

GoogleCodeExporter commented 8 years ago
Well, I've just had a chance to try, but this stopped me (see the attached 
file). Scary, isn't it? It's the first time I'm seeing anything like this.

Original comment by tomasz.p...@gmail.com on 15 Feb 2013 at 10:51

Attachments:

GoogleCodeExporter commented 8 years ago
I'm not an Android dev, but dalvik is complaining it doesn't find the Mockito 
classes in the APK, maybe there's something wrong there to look at first. If 
this is not the problem then ... more time will be needed to see what's going 
on.

Original comment by brice.du...@gmail.com on 15 Feb 2013 at 1:06

GoogleCodeExporter commented 8 years ago
It appears that my whole dev environment suddenly became pretty much unstable. 
If I get it to work soon, I'll check the latest Objenesis.

Original comment by tomasz.p...@gmail.com on 15 Feb 2013 at 1:22

GoogleCodeExporter commented 8 years ago
In my case issue occurs only if I use exception class as parameter for 
doThrow():

doThrow(MyException.class).when(myMock).myMethod()  --- error

When I replace exception class to direct exception instance error disappear:

doThrow(new MyException()).when(myMock).myMethod()  --- pass

Original comment by roman.gr...@gmail.com on 24 Sep 2013 at 3:00

GoogleCodeExporter commented 8 years ago
I'm merging this issue in 414 because this is due to the old objenesis lib, now 
that Objenesis has shown code working on dalvik, i think it's time to upgrade

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 3:51