Problems with mocking Mozilla Rhino - mocking seems to work only partially...

188 views
Skip to first unread message

Marcel Overdijk

unread,
Oct 6, 2011, 7:26:39 AM10/6/11
to mockito
Here is a snippet of my junit test:

cx = mock(Context.class);
global = mock(Global.class);
scope = mock(ScriptableObject.class);
envJs = mock(File.class);
envJsReader = mock(FileReader.class);

mockStatic(Context.class);
given(Context.enter()).willReturn(cx);

whenNew(Global.class).withNoArguments().thenReturn(global);

when(cx.initStandardObjects(global)).thenReturn(scope);


whenNew(FileReader.class).withArguments(envJs).thenReturn(envJsReader);
System.out.println("cx = " + cx);
when(cx.evaluateReader(any(Scriptable.class),
any(FileReader.class), anyString(), anyInt(),
any())).thenReturn(null);


What I not understand is that despite the cx object is mock, the call
cx.evaluateReader(..) goed actually inside the real class.

See stacktrace:


cx = Mock for Context, hashCode: 505588567
java.lang.IllegalStateException: FAILED ASSERTION
at org.mozilla.javascript.Kit.codeBug(Kit.java:449)
at org.mozilla.javascript.Context.compileImpl(Context.java:2345)
at org.mozilla.javascript.Context.compileReader(Context.java:1321)
at org.mozilla.javascript.Context.compileReader(Context.java:1293)
at org.mozilla.javascript.Context.evaluateReader(Context.java:1132)
at
com.googlecode.lesscss4j.LessCompilerTest.testInit(LessCompilerTest.java:
133)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
307)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
86)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
94)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:
294)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:
112)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:
73)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:
282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
207)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:
146)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
34)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
44)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:
118)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:
102)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:
53)
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)


The actual call is:

cx.evaluateReader(scope, new FileReader(envJs), "env.rhino.js", 1,
null);
(note that the evaluateReader actually returns something)


Any ideas?

Brice Dutheil

unread,
Oct 6, 2011, 8:12:14 AM10/6/11
to moc...@googlegroups.com
Hi,

Before answering your question I must remind you this guideline : Don't mock type you don't own!
And your test setup seems to be a bit complex, you might want to simplify it. Using many mocks in one test method seems like a test smell in my opinion.


So, for the answer : Mockito can't mock final methods/classes, it's explained in the faq :)

Context.evaluateReader(...) seems to be final according to the source here http://www.java2s.com/Open-Source/Java-Document/Scripting/rhino-1.7/org/mozilla/javascript/Context.java.htm

As you are already using powermock, I suggest to use it to prepare Context.class.
-- 
Brice



--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


Marcel Overdijk

unread,
Oct 6, 2011, 8:16:37 AM10/6/11
to mockito
Thanks for quick reply Brice,

I take your comments for granted.
However I'm already using PowerMock and used it to prepare the
Context.class. But no luck.


On Oct 6, 2:12 pm, Brice Dutheil <brice.duth...@gmail.com> wrote:
> Hi,
>
> Before answering your question I must remind you this guideline : Don't mock
> type you don't own!
> And your test setup seems to be a bit complex, you might want to simplify
> it. Using many mocks in one test method seems like a test smell in my
> opinion.
>
> So, for the answer : Mockito can't mock final methods/classes, it's
> explained in the faq :)
>
> Context.evaluateReader(...) seems to be final according to the source herehttp://www.java2s.com/Open-Source/Java-Document/Scripting/rhino-1.7/o...
>
> As you are already using powermock, I suggest to use it to prepare
> Context.class.
> --
> Brice
>

Brice Dutheil

unread,
Oct 6, 2011, 8:28:27 AM10/6/11
to moc...@googlegroups.com
I don't use powermock at all, but you should verify that you are using the correct static calls then.

PowerMockito.mock(...)
PowerMockito.createMock(...)

You should check on there wiki.

-- 
Brice

Marcel Overdijk

unread,
Oct 6, 2011, 8:34:47 AM10/6/11
to mockito
Damn damn ;-)

Thanks Brice you pointed me in the correct direction. The static mock
import was from Mockito and not from Powermock.
Kept reading over that one without noticing.

Thanks for your help, much appreciated!

On Oct 6, 2:28 pm, Brice Dutheil <brice.duth...@gmail.com> wrote:
> I don't use powermock at all, but you should verify that you are using the
> correct static calls then.
>
> PowerMockito.mock(...)
>
> PowerMockito.createMock(...)
>
> You should check on there wiki.
>
> --
> Brice
>

Brice Dutheil

unread,
Oct 6, 2011, 8:52:31 AM10/6/11
to moc...@googlegroups.com
You're welcome :)

-- 
Brice
Reply all
Reply to author
Forward
0 new messages