chiffchaff / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

ExceptionInInitializerError with RhinoScriptEngineFactory.getScriptEngine() when using PrepareForTest annotation #466

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create two tests within a class annotated with 
@RunWith(PowerMockRunner.class)
2. Annotate each test with @PrepareForTest()
3. Attempt to retrieve a script engine
4. Run both tests
5. See below for sample code

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

I would expect both tests to pass.

Instead, one test will succeed and the other will fail.  Something is happening 
with the PrepareForTest annotation that does not play nicely with 
ScriptEngineManager.getEngineByName().  If you remove the PrepareForTest 
annotations, both tests pass.  If you move the annotation to the class level, 
any other test class in the project that also uses the script engine will fail.

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

PowerMock 1.5.1 on Windows 7.
JDK 7

Please provide any additional information below.

Sample test code:

@RunWith(PowerMockRunner.class)
public class PowerMockTest {

    @Test
    @PrepareForTest()
    public void test1() throws Exception {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
        assertNotNull(scriptEngine);
    }

    @Test
    @PrepareForTest()
    public void test2() throws Exception {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
        assertNotNull(scriptEngine);
    }
}

Original issue reported on code.google.com by mikedeha...@gmail.com on 14 Oct 2013 at 3:02

Attachments:

GoogleCodeExporter commented 9 years ago
Here is a stack trace of the error:

java.lang.ExceptionInInitializerError
    at com.sun.script.javascript.RhinoScriptEngineFactory.getScriptEngine(RhinoScriptEngineFactory.java:74)
    at javax.script.ScriptEngineManager.getEngineByName(ScriptEngineManager.java:243)
    at powermock.test.PowerMockTest.test2(PowerMockTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    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:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    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:101)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.IllegalStateException
    at sun.org.mozilla.javascript.internal.ContextFactory.initGlobal(ContextFactory.java:209)
    at com.sun.script.javascript.RhinoScriptEngine.<clinit>(RhinoScriptEngine.java:71)
    ... 35 more

Original comment by mikedeha...@gmail.com on 14 Oct 2013 at 3:06

GoogleCodeExporter commented 9 years ago
I had the same issue. I was able to work around it by annotating the test class 
with:
@PowerMockIgnore({"javax.script.*", "com.sun.script.*"})

Original comment by BruceMFr...@gmail.com on 5 Jun 2015 at 5:59