{"action":"created","issue":{"url":"https://api.github.com/repos/mockito/mockito/issues/133","labels_url":"https://api.github.com/repos/mockito/mockito/issues/133/labels{/name}","comments_url":"https://api.github.com/repos/mockito/mockito/issues/133/comments","events_url":"https://api.github.com/repos/mockito/mockito/issues/133/events","html_url":"https://github.com/mockito/mockito/issues/133","id":51603458,"number":133,"title":"Mockito can't spy on object loaded in parent classloader","user":{"login":"akomisarek","id":8721789,"avatar_url":"https://avatars.githubusercontent.com/u/8721789?v=3","gravatar_id":"","url":"https://api.github.com/users/akomisarek","html_url":"https://github.com/akomisarek","followers_url":"https://api.github.com/users/akomisarek/followers","following_url":"https://api.github.com/users/akomisarek/following{/other_user}","gists_url":"https://api.github.com/users/akomisarek/gists{/gist_id}","starred_url":"https://api.github.com/users/akomisarek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akomisarek/subscriptions","organizations_url":"https://api.github.com/users/akomisarek/orgs","repos_url":"https://api.github.com/users/akomisarek/repos","events_url":"https://api.github.com/users/akomisarek/events{/privacy}","received_events_url":"https://api.github.com/users/akomisarek/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2014-12-10T19:57:49Z","updated_at":"2015-01-03T13:54:47Z","closed_at":null,"body":"I have encountered issue with Mockito (I have tested this on 1.10.14) and usage of different classloaders - running tests in SBT 0,13.\r\n\r\nI have got empty class *TestClass* and following test:\r\n```Java\r\n    @Test\r\n    public void runTest() {\r\n        Mockito.spy(new TestClass(){\r\n\r\n        });\r\n    }\r\n```\r\nThis test fails with following: \r\n```\r\nCaused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null\r\n[error]     at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)\r\n[error]     at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)\r\n[error]     at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)\r\n[error]     at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:123)\r\n[error]     at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)\r\n[error]     at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)\r\n[error]     at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)\r\n[error]     at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)\r\n[error]     at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)\r\n[error]     at org.mockito.Mockito.spy(Mockito.java:1367)\r\n[error]     ... 56 more\r\n[error] Caused by: java.lang.reflect.InvocationTargetException\r\n[error]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n[error]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\r\n[error]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n[error]     at java.lang.reflect.Method.invoke(Method.java:606)\r\n[error]     at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)\r\n[error]     at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)\r\n[error]     ... 65 more\r\n[error] Caused by: java.lang.IllegalAccessError: class TestMock$1$$EnhancerByMockitoWithCGLIB$$7c797c20 cannot access its superclass TestMock$1\r\n[error]     at java.lang.ClassLoader.defineClass1(Native Method)\r\n[error]     at java.lang.ClassLoader.defineClass(ClassLoader.java:800)\r\n```\r\nLooks like there is class loader issue - in sbt, the default classloader is the one for the sbt launcher, but you are \"run\" in a sub classloader.\r\n\r\nFollowing calls give different result: *Thread.currentThread().getContextClassLoader().getClass();* and *TestClass.class.getClassLoader().getClass();* \r\nThe second one is the parent of the first one.\r\n\r\nOf course there is no issue running this piece of code in IDE, but in my opinion this is Mockito's problem, something which could be improved. \r\n \r\nIs it possible to have this fixed in Mockito? I would say so because changing current classloader to the one used to load the class solves the issue. "},"comment":{"url":"https://api.github.com/repos/mockito/mockito/issues/comments/68595645","html_url":"https://github.com/mockito/mockito/issues/133#issuecomment-68595645","issue_url":"https://api.github.com/repos/mockito/mockito/issues/133","id":68595645,"user":{"login":"bric3","id":803621,"avatar_url":"https://avatars.githubusercontent.com/u/803621?v=3","gravatar_id":"","url":"https://api.github.com/users/bric3","html_url":"https://github.com/bric3","followers_url":"https://api.github.com/users/bric3/followers","following_url":"https://api.github.com/users/bric3/following{/other_user}","gists_url":"https://api.github.com/users/bric3/gists{/gist_id}","starred_url":"https://api.github.com/users/bric3/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bric3/subscriptions","organizations_url":"https://api.github.com/users/bric3/orgs","repos_url":"https://api.github.com/users/bric3/repos","events_url":"https://api.github.com/users/bric3/events{/privacy}","received_events_url":"https://api.github.com/users/bric3/received_events","type":"User","site_admin":false},"created_at":"2015-01-03T13:54:47Z","updated_at":"2015-01-03T13:54:47Z","body":"Play Framework users had a similar issue a while ago, now I know they are using SBT, it sometime ago but a workaround was to disable classcache in objenesis, maybe it can resolve your issue. We will be happy with the feedback.\r\n\r\nThis can be done easily with a configuration class that you can use in your test classpath. See the [`IMockitoConfiguration javadoc`](http://site.mockito.org/mockito/docs/current/org/mockito/configuration/IMockitoConfiguration.html)\r\n\r\nYour implementation will have to always return `false` in this method [`enableClassCache()`](http://site.mockito.org/mockito/docs/current/org/mockito/configuration/IMockitoConfiguration.html#enableClassCache())\r\n"}}