[英]IllegalAccessError when accessing package private class in JMock
I have written a unit test using JMock 2.6.0-RC2 that makes use of several package private classes. 我已经使用JMock 2.6.0-RC2编写了单元测试,该测试利用了几个程序包私有类。 For some reason, this causes an
IllegalAccessError
: 由于某些原因,这将导致
IllegalAccessError
:
java.lang.IllegalAccessError: tried to access class foo.PackagePrivateClass from class $Proxy6
at $Proxy6.getInstance(Unknown Source)
at foo.UsingClass.<init>(UsingClass.java:6)
at foo.FailingTest.testFailure(FailingTest.java:29)
I have uploaded a minimal Maven project that demonstrates the issue to here: http://dl.dropbox.com/u/108474287/example-project.zip . 我已经在此处上传了一个最小的Maven项目,以演示该问题: http : //dl.dropbox.com/u/108474287/example-project.zip 。 The relevant code is also shown below (it's fairly concise).
相关代码也显示在下面(非常简洁)。
Why am I seeing an error in my test? 为什么在测试中看到错误? The test class is part of the same package as the tested classes, so I would not expect access control to be an issue.
测试类与测试类属于同一包,因此我不希望访问控制成为问题。
Some interesting observations: 一些有趣的观察:
If I change ExampleInterface
to be package private (rather than public), the problem goes away. 如果我将
ExampleInterface
更改为包私有(而不是公共),则问题会消失。 Sadly, in my real-life project this is not a possibility. 可悲的是,在我的现实生活中,这是不可能的。
The exception only occurs if I return a value from my mock object. 仅当我从模拟对象返回值时,才会发生异常。 Those who download my example project will see another test in which
null
is returned; 下载我的示例项目的人将看到另一个测试,该测试返回
null
。 this test passes. 这个测试通过了。
If I move the all the classes into the default package, the tests pass! 如果我将所有类都移到默认包中,则测试通过! If all the classes remain in package
foo
, the tests fail. 如果所有类都保留在包
foo
,则测试将失败。
Below are the files contained in my project. 以下是我的项目中包含的文件。 The ZIP I linked to above is a small Maven project that houses all of these.
我上面链接的ZIP是一个小型Maven项目,其中包含所有这些内容。
I have also posted this question to the JMock developer list. 我还将此问题发布到了JMock开发人员列表中。 I will update this question if I receives answers there (and vice versa).
如果我在那里收到答案,我将更新此问题(反之亦然)。
FailingTest.java FailingTest.java
package foo;
// Imports omitted
@RunWith(JMock.class)
public class FailingTest {
private static final PackagePrivateClass EXAMPLE_INSTANCE =
new PackagePrivateClass();
public Mockery context = new JUnit4Mockery();
@Test
public void testFailure() {
final ExampleInterface exampleInterface = context
.mock(ExampleInterface.class);
context.checking(new Expectations() {
{
oneOf(exampleInterface).getInstance();
will(returnValue(EXAMPLE_INSTANCE));
}
});
new UsingClass(exampleInterface); <-- exception thrown from constructor
}
}
ExampleInterface.java ExampleInterface.java
package foo;
public interface ExampleInterface {
PackagePrivateClass getInstance();
}
PackagePrivateClass.java PackagePrivateClass.java
package foo;
class PackagePrivateClass {
PackagePrivateClass() {}
@Override
public String toString() {
return "Hello, World!";
}
}
UsingClass.java usingClass.java
package foo;
class UsingClass {
UsingClass(ExampleInterface exampleInterface) {
PackagePrivateClass bar = exampleInterface.getInstance(); // <--- exception
System.out.println(bar); // originates here
}
}
To provide an update for future readers.... 为将来的读者提供更新。
Currently, I believe this is a bug in JMock. 目前,我相信这是JMock中的错误。 I've filed it on the JMock github page: https://github.com/jmock-developers/jmock-library/issues/43 .
我已经将其归档在JMock github页面上: https : //github.com/jmock-developers/jmock-library/issues/43 。 No responses yet (nor to my comment on the mailing list).
尚无回复(我对邮件列表的评论也没有)。
I've since switched to Mockito , which is able to handle exactly the same situation without error. 从那以后 ,我切换到Mockito ,它能够处理完全相同的情况而不会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.