簡體   English   中英

如何在Maven構建中訪問受程序包保護的類?

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

一些代碼返回了一個我試圖模擬的程序包保護類型。 我使用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返回一個受程序包保護的類作為迭代器,但是您該怎么辦?

真正的問題是,您試圖模擬ItemCollection並使其返回真實對象。 不幸的是,您能夠解決此問題的唯一方法是使用PowerMock:

https://code.google.com/p/powermock/wiki/ 動機https://code.google.com/p/powermock/wiki/BypassEncapsulation

使用PowerMockRunner時 ,您所要做的就是將包私有類放在@PrepareForTest ,它應該為您處理。 如果您還不夠,請在評論中告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM