【问题标题】:How do I access a package protected class in Maven build?如何在 Maven 构建中访问包保护类?
【发布时间】:2015-07-16 20:55:24
【问题描述】:

一些代码返回我试图模拟的包保护类型。我使用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$$14504e4e.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$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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 返回一个包保护类作为迭代器,但是你能做什么呢?

【问题讨论】:

    标签: java maven amazon-web-services easymock


    【解决方案1】:

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

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

    在使用PowerMockRunner 时,您所要做的就是将包私有类放在@PrepareForTest 中,它应该会为您处理。如果这还不够,请在 cmets 中告诉我。

    【讨论】:

    • 我已经深入研究了一下,似乎包访问受到用于运行测试的 Java 安全策略的限制。 PowerMock 能否绕过这个问题?
    • @Max 是的。阅读链接
    猜你喜欢
    • 1970-01-01
    • 2015-11-05
    • 2011-05-23
    • 2016-10-23
    • 2016-05-06
    • 1970-01-01
    • 2014-08-29
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多