java - How do I access a package protected class in Maven build? -
some code returns package protected type trying mock. doing using class.forname
, maven throws illegalaccesserror
now.
here sample:
class<?> itemsupportclasss = class.forname("com.amazonaws.services.dynamodbv2.document.internal.iteratorsupport"); iterator<item> mockiterator = (iterator<item>) createmock(itemsupportclasss); expect(mockiterator.hasnext()).andreturn(false); itemcollection<queryoutcome> itemcollection = createmock(itemcollection.class); expect(((iterable<item>) itemcollection).iterator()).andreturn(mockiterator); // , on
when itemcollection.iterator()
called, following error:
java.lang.illegalaccesserror: com/amazonaws/services/dynamodbv2/document/internal/iteratorsupport @ com.amazonaws.services.dynamodbv2.document.itemcollection$$enhancerbycglib$$14504e4e.iterator(<generated>) @ com.mycom.myclass(myclass.java:77) @ com.mycom.myclass(myclass.java:230) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:78) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.apache.maven.surefire.junit4.junit4provider.execute(junit4provider.java:252) @ org.apache.maven.surefire.junit4.junit4provider.executetestset(junit4provider.java:141) @ org.apache.maven.surefire.junit4.junit4provider.invoke(junit4provider.java:112) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.apache.maven.surefire.util.reflectionutils.invokemethodwitharray(reflectionutils.java:189) @ org.apache.maven.surefire.booter.providerfactory$providerproxy.invoke(providerfactory.java:165) @ org.apache.maven.surefire.booter.providerfactory.invokeprovider(providerfactory.java:85) @ org.apache.maven.surefire.booter.forkedbooter.runsuitesinprocess(forkedbooter.java:115) @ org.apache.maven.surefire.booter.forkedbooter.main(forkedbooter.java:75)
of course, root issue dynamo db returns package protected class iterator, can do?
the real problem here trying mock itemcollection
, have return real object. way you're going able solve problem, unfortunately, using powermock:
https://code.google.com/p/powermock/wiki/motivation https://code.google.com/p/powermock/wiki/bypassencapsulation
all have do, when using powermockrunner, put package private class in @preparefortest
, should handle you. if not enough let me know in comments.
Comments
Post a Comment