{"action":"created","issue":{"url":"https://api.github.com/repos/mockito/mockito/issues/368","repository_url":"https://api.github.com/repos/mockito/mockito","labels_url":"https://api.github.com/repos/mockito/mockito/issues/368/labels{/name}","comments_url":"https://api.github.com/repos/mockito/mockito/issues/368/comments","events_url":"https://api.github.com/repos/mockito/mockito/issues/368/events","html_url":"https://github.com/mockito/mockito/issues/368","id":139585823,"number":368,"title":"Class cast exception in PluginLoader.loadPlugin","user":{"login":"thekingnothing","id":13175296,"avatar_url":"https://avatars.githubusercontent.com/u/13175296?v=3","gravatar_id":"","url":"https://api.github.com/users/thekingnothing","html_url":"https://github.com/thekingnothing","followers_url":"https://api.github.com/users/thekingnothing/followers","following_url":"https://api.github.com/users/thekingnothing/following{/other_user}","gists_url":"https://api.github.com/users/thekingnothing/gists{/gist_id}","starred_url":"https://api.github.com/users/thekingnothing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thekingnothing/subscriptions","organizations_url":"https://api.github.com/users/thekingnothing/orgs","repos_url":"https://api.github.com/users/thekingnothing/repos","events_url":"https://api.github.com/users/thekingnothing/events{/privacy}","received_events_url":"https://api.github.com/users/thekingnothing/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/mockito/mockito/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-03-09T14:06:16Z","updated_at":"2016-07-03T20:56:17Z","closed_at":null,"body":"I've been working on two defects for PowerMock ([first]( https://github.com/jayway/powermock/issues/634) and [second](https://github.com/jayway/powermock/issues/632)). Both has very similar stack trace: \r\n\r\n> java.lang.ExceptionInInitializerError\r\n> at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.(ConditionalStackTraceFilter.java:17)\r\n> at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)\r\n> at org.mockito.exceptions.base.MockitoException.(MockitoException.java:30)\r\n> at org.mockito.exceptions.misusing.MockitoConfigurationException.(MockitoConfigurationException.java:18)\r\n> at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)\r\n> at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)\r\n> at org.mockito.internal.configuration.plugins.PluginRegistry.(PluginRegistry.java:12)\r\n> at org.mockito.internal.configuration.plugins.Plugins.(Plugins.java:11)\r\n> at org.mockito.internal.util.MockUtil.(MockUtil.java:24)\r\n> at org.mockito.internal.MockitoCore.(MockitoCore.java:44)\r\n> at org.mockito.Mockito.(Mockito.java:975)\r\n> at org.powermock.modules.test.junit4.rule.objenesis.MockStaticNotPreparedTest.testWhenNotPrepared(MockStaticNotPreparedTest.java:45)\r\n> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n> at java.lang.reflect.Method.invoke(Method.java:498)\r\n> at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)\r\n> at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)\r\n> at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)\r\n> at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)\r\n> at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:65)\r\n> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n> at java.lang.reflect.Method.invoke(Method.java:498)\r\n> at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1898)\r\n> at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:801)\r\n> at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:666)\r\n> at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)\r\n> at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)\r\n> at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)\r\n> at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:57)\r\n> at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110)\r\n> at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)\r\n> at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)\r\n> at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)\r\n> at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)\r\n> at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)\r\n> at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)\r\n> at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)\r\n> at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)\r\n> at org.junit.runners.ParentRunner.run(ParentRunner.java:236)\r\n> at org.junit.runner.JUnitCore.run(JUnitCore.java:157)\r\n> at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)\r\n> at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)\r\n> at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)\r\n> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n> at java.lang.reflect.Method.invoke(Method.java:498)\r\n> at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)\r\n> Caused by: java.lang.NullPointerException\r\n> at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)\r\n> at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.(StackTraceFilter.java:21)\r\n> ... 51 more\r\n\r\nAs @bric3 recommend in issue #355 I had started debug `PluginLoader` and I found class cast exception in `org.mockito.internal.configuration.plugins.PluginLoader#loadImpl`. And this class cast exception is transformed to NPE. Main reason of class class exception, that plugin interface class and plugin implementor could be loaded by different class loader, because for plugin implementor current context class loader is used. \r\n\r\nI've create the [gist](https://gist.github.com/thekingnothing/9674b6196f91b67aa944) with test which demonstrate the issue. \r\n\r\nIssue could be reproduce on both latest version:  1.10.19 2, 2.0.43-beta\r\n\r\nI'm going to  raise a pull request soon for Mockito 2, but I think hot-fix also should be release for 1.10.\r\n\r\nTest code: \r\n\r\n```\r\n\r\n/**\r\n *  Test PluginLoader for case when Plugin class is loaded via class loader which is different from current context\r\n *  class loader.\r\n */\r\npublic class PluginLoaderTest extends TestBase {\r\n\r\n    @InjectMocks\r\n    private PluginLoader pluginLoader;\r\n\r\n    @Mock\r\n    private PluginSwitch pluginSwitch;\r\n\r\n    @Before\r\n    public void setUp() throws Exception {\r\n        given(pluginSwitch.isEnabled(anyString())).willReturn(true);\r\n    }\r\n\r\n    @Test\r\n    public void without_custom_context_class_loader_plugin_should_be_load() throws Exception {\r\n\r\n\r\n        Object pluginInterface = pluginLoader.loadPlugin(PluginInterface.class, \"org.mockito.internal\" +\r\n                                                                                                  \".configuration.plugins.DefaultPlugin\");\r\n\r\n        assertEquals(PluginImpl.class, pluginInterface.getClass());\r\n    }\r\n\r\n    @Test\r\n    public void if_custom_context_class_loader_is_used_then_plugin_should_be_load() throws Exception {\r\n\r\n        ClassLoader classLoader = ClassLoaders.inMemoryClassLoader().withParent(getParentClassLoader()).withClassDefinition(\"\", readResource()).build();\r\n\r\n        Thread.currentThread().setContextClassLoader(classLoader);\r\n\r\n        Object pluginInterface = pluginLoader.loadPlugin(PluginInterface.class.getName(), \"org.mockito.internal\" +\r\n                                                                                                  \".configuration.plugins.DefaultPlugin\");\r\n\r\n        assertEquals(classLoader.loadClass(PluginImpl.class.getName()), pluginInterface.getClass());\r\n    }\r\n\r\n    private ClassLoader getParentClassLoader() {\r\n        return ClassLoaders.excludingClassLoader().withCodeSourceUrlOf(PluginImpl.class).build();\r\n    }\r\n\r\n    private byte[] readResource() {\r\n\r\n        InputStream inputStream = ClassLoaders.currentClassLoader().getResourceAsStream(\"mockito-extensions/org.mockito.internal.configuration.plugins.PluginInterface\");\r\n        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();\r\n\r\n        try {\r\n            int nRead = 0;\r\n            byte[] buffer = new byte[1024];\r\n\r\n            while ((nRead = inputStream.read(buffer, 0, buffer.length)) != -1) {\r\n                outputStream.write(buffer, 0, nRead);\r\n            }\r\n\r\n            outputStream.flush();\r\n\r\n            return outputStream.toByteArray();\r\n        } catch (IOException e) {\r\n            throw new RuntimeException(\"Cannot read test resource\", e);\r\n        } finally {\r\n            IOUtil.close(inputStream);\r\n            IOUtil.close(outputStream);\r\n        }\r\n\r\n    }\r\n\r\n} \r\n```"},"comment":{"url":"https://api.github.com/repos/mockito/mockito/issues/comments/230174894","html_url":"https://github.com/mockito/mockito/issues/368#issuecomment-230174894","issue_url":"https://api.github.com/repos/mockito/mockito/issues/368","id":230174894,"user":{"login":"TimvdLippe","id":5948271,"avatar_url":"https://avatars.githubusercontent.com/u/5948271?v=3","gravatar_id":"","url":"https://api.github.com/users/TimvdLippe","html_url":"https://github.com/TimvdLippe","followers_url":"https://api.github.com/users/TimvdLippe/followers","following_url":"https://api.github.com/users/TimvdLippe/following{/other_user}","gists_url":"https://api.github.com/users/TimvdLippe/gists{/gist_id}","starred_url":"https://api.github.com/users/TimvdLippe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TimvdLippe/subscriptions","organizations_url":"https://api.github.com/users/TimvdLippe/orgs","repos_url":"https://api.github.com/users/TimvdLippe/repos","events_url":"https://api.github.com/users/TimvdLippe/events{/privacy}","received_events_url":"https://api.github.com/users/TimvdLippe/received_events","type":"User","site_admin":false},"created_at":"2016-07-03T20:56:17Z","updated_at":"2016-07-03T20:56:17Z","body":"Since #370 has been closed, @raphw did you resolve this issue as indicated in https://github.com/mockito/mockito/pull/370#issuecomment-227023691 ?"}}
{"actor":{"display_login":"TimvdLippe"}}