1

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:

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?
jmgoyesc
  • 2,677
  • 1
  • 18
  • 16
  • 3
    Is it possible that you are using different Java/jdk versions in IntelliJ and on the command line? It might be [this issue](https://stackoverflow.com/a/39120603/2711488) that has been fixed in JDK8, update 102. Likewise, it might be that the problem disappears when compiling with ECJ instead of `javac`; I’m not sure which compiler IntelliJ uses or whether this is configurable. – Holger Nov 13 '17 at 13:48
  • You are right. In command line I was using jdk 1.8.51 and in IntelliJ I was using jdk 1.8.111 – jmgoyesc Nov 13 '17 at 16:22

0 Answers0