如何在 Maven 构建中访问受保护的包 class?

How do I access a package protected class in Maven build?

一些代码 returns 我试图模拟的包保护类型。我这样做是使用 Class.forName,但 Maven 现在抛出一个 IllegalAccessError

这是一个示例:

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);

// And so on

调用 itemCollection.iterator() 时,出现以下错误:

java.lang.IllegalAccessError: com/amazonaws/services/dynamodbv2/document/internal/IteratorSupport
    at com.amazonaws.services.dynamodbv2.document.ItemCollection$$EnhancerByCGLIB$504e4e.iterator(<generated>)
    at com.mycom.MyClass(MyClass.java:77)
    at com.mycom.MyClass(MyClass.java:230)
    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:483)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    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:483)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

当然,根本问题是 Dynamo DB returns 作为迭代器受到保护的包 class,但是您能做什么呢?

这里真正的问题是您试图模拟 ItemCollection 并让它 return 成为一个真实的对象。不幸的是,您能够解决此问题的唯一方法是使用 PowerMock:

https://code.google.com/p/powermock/wiki/Motivation https://code.google.com/p/powermock/wiki/BypassEncapsulation

使用 PowerMockRunner, is put the package private class in @PrepareForTest 时您需要做的所有事情,它应该会为您处理。如果这还不够,请在评论中告诉我。