I have two libraries. One contains the model and the other contains something which use the model. Both are in separate jar files.
Model contains (notice that AbstractClass has package visibility)
public class PublicClass extends AbstractClass { }
.
abstract class AbstractClass {
private String id;
private Long key;nter code here
}
Other project contains a test which use this model.
This test is working
@Test
public void testIssue02() {
String message = elements.stream()
.map(a -> publicDao.findById(a.getId()))
.map(Object::toString)
.collect(Collectors.joining(", "));
Assert.assertEquals("...", message);
}
This test is failing
@Test
public void testIssue01() {
//The problem is because package visibility
String message = elements.stream()
.map(PublicClass::getId)
.map(publicDao::findById)
.map(Object::toString)
.collect(Collectors.joining(", "));
Assert.assertEquals("...", message);
}
The difference between both test cases is testIssue01() is using method reference to get id from PublicClass meanwhile testIssue02() is getting the property directly. The error that I got is:
Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.09 sec <<< FAILURE!
testIssue01(com.personal.sample.lambda.issue.SimpleIssueTest) Time elapsed: 0.05 sec <<< ERROR!
java.lang.IllegalAccessError: tried to access class com.personal.sample.test.other.AbstractClass from class com.personal.sample.lambda.issue.SimpleIssueTest
at com.personal.sample.lambda.issue.SimpleIssueTest.lambda$testIssue01$0(SimpleIssueTest.java:27)
Both are maven projects and I only got the error when I execute in command line "mvn clean install" which is going to execute the unit tests. I also using IntelliJ to edit the project and if I run the test from IntelliJ, both test cases worked.
I have created both project in github to replicate the case:
- method-reference-model: Contains the model
- method-reference-test: Contains the unit tests
My questions are:
- Why does it work in IntelliJ and fail in console?
R. Thanks to Holger. In command line I was compiling with jdk 1.8.51 and there is a known issue.
- What is the reason of this error?