Howto add spy to MappedByteBuffer?

26 views
Skip to first unread message

Alexander Pelepeychenko

unread,
Nov 13, 2015, 12:21:09 AM11/13/15
to PowerMock
How to add spy to MappedByteBuffer?

When i try to spy my MappedByteBuffer object, then exception was thrown:

org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.nio.DirectByteBuffer
Mockito can only mock visible & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
    at org
.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:131)
    at org
.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
    at org
.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)
    at org
.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:59)
    at org
.powermock.api.mockito.PowerMockito.spy(PowerMockito.java:220)
    at misat
.rounddb.EnsurePowerMockTest.addSpyTest(EnsurePowerMockTest.java:34)
    at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java
.lang.reflect.Method.invoke(Method.java:497)
    at org
.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org
.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org
.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org
.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org
.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org
.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org
.testng.TestRunner.privateRun(TestRunner.java:774)
    at org
.testng.TestRunner.run(TestRunner.java:624)
    at org
.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org
.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org
.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org
.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org
.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org
.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org
.testng.TestNG.runSuitesSequentially(TestNG.java:1191)
    at org
.testng.TestNG.runSuitesLocally(TestNG.java:1116)
    at org
.testng.TestNG.run(TestNG.java:1024)
    at org
.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
    at org
.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
    at org
.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
    at org
.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
    at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java
.lang.reflect.Method.invoke(Method.java:497)
    at com
.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at org
.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)
    at org
.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
    at org
.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
    at org
.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123)
   
... 35 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java
.lang.reflect.Method.invoke(Method.java:497)
    at org
.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)
    at org
.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)
   
... 38 more
Caused by: java.lang.IllegalAccessError: class $java.nio.DirectByteBuffer$$EnhancerByMockitoWithCGLIB$$d5fb8f36 cannot access its superclass java.nio.DirectByteBuffer
    at java
.lang.ClassLoader.defineClass1(Native Method)
    at java
.lang.ClassLoader.defineClass(ClassLoader.java:760)
   
... 44 more



My test code:

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import static org.powermock.api.mockito.PowerMockito.*;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.testng.Assert.assertTrue;

@PrepareForTest({MappedByteBuffer.class, RandomAccessFile.class})
public class EnsurePowerMockTest extends PowerMockTestCase{

   
@Test
   
public void addSpyTest() throws IOException {
       
RandomAccessFile raf = new RandomAccessFile("testFile1.tmp", "rw");
       
FileChannel fc = raf.getChannel();
       
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, 100);
       
MappedByteBuffer mockMbb = spy(mbb); //org.mockito.exceptions.base.MockitoException
       
   
}
}



Johan Haleby

unread,
Nov 13, 2015, 12:42:29 AM11/13/15
to powe...@googlegroups.com
You may need to use this approach?

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages