0
**Description** 

Access restriction: The type Test is not accessible due to restriction on required library 

**Resource**

/Applications/Eclipse Java/plugins/org.junit_4.11.0.v201303080030/junit.jar 

**Path**

AdditionTest2.java  

**Location**

/Practice/src/testing   line 5  

**Type**

Java Problem

I've seen the answers to these other SO threads:

Access restriction on class due to restriction on required library

Access restriction on class due to restriction on required library rt.jar?

and I've applied the fix - Build Path --> Remove System Library --> Add it back in, and it works. However, I still have two problems:

1.Why is the code still highlighted in red?

Btw, the code is as follows

package testing;

import static org.junit.Assert.*;

import org.junit.Test;

public class AdditionTest2 {
    // @Test is underlined red
@Test
public void testAddition() {
    Addition add = new Addition();
    int[] numbers = {1, 2};

            // this line is underlined red
    assertEquals("1 + 2 must be 3", 3, Addition.add(numbers));
}

}

2.What caused this error? Again, I've read the two threads, but I still don't understand it.

Thanks!

Baggio

EDIT: Actually, I stand corrected - it DOESN'T work. I'm not too familiar with testing in eclipse, but under the JUnit tab, nothing happens.

EDIT 2: Build Path image if that helps any Build Path

EDIT 3: Build Path image for JUnit 3 and JUnit 4

Build Path for JUnit 3 and JUnit 4

EDIT 4 + Solution: Actually, the testing code with JUnit 4 doesn't run at all - when I click Run --> Run As it didn't run - I tried to remove JUnit 3 from the Build Path, and it works now! Except now I have the other problem of not knowing why the test failed, but that's another problem.

Community
  • 1
  • 1
blazonix
  • 393
  • 2
  • 5
  • 15
  • What does the build path entry for JUnit look like? Look into `.classpath` and append the line to your question. – Aaron Digulla May 20 '14 at 07:17
  • Do you see something in the "Console" view? – Jens May 20 '14 at 07:19
  • @AaronDigulla Sorry, I'm not too familiar with build paths and such - do you mean Project --> Properties --> Java Build Path? If so, I've posted it above. Sorry, I'm on a Mac, how do I show .classpath? – blazonix May 20 '14 at 07:28
  • @blazonix: Close :-) We're more interested with the stuff under "JUnit 3". – Aaron Digulla May 20 '14 at 07:33

4 Answers4

1

Eclipse is based on OSGi. OSGi has a notion of "yes, you can see this class but you better don't use it." It's an extension of private classes.

The general idea is that code from someone contains public API and it contains classes which are public because the Java compiler requires it but they aren't part of the public API.

Examples are classes in the Java runtime which are in the com.sun package. Since only Oracle's version of Java contains them, it would be bad for you to use them. That's why Eclipse developers added the check to their compiler.

In your case, the error message seems odd. junit.jar doesn't contain private API. My guess is that you somehow pulled in the wrong JUnit JAR (ie. one which Eclipse thinks is private and only accessible for the IDE).

Try to use Built Path--> Add Libraries to add "JUnit" to your project instead of manually adding a JAR.

[EDIT] If you use Built Path--> Add Libraries to add the JUnit JAR, then this error shouldn't happen. In fact, it shouldn't be an error in the first place - Access restrictions are warnings.

Try to reset your compiler options to the default and try again.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • @AaronDiguilla - Actually, I tried that (removing JUnit 3 and then adding it back, removing JUnit 3 and adding JUnit 4), it didn't work. – blazonix May 20 '14 at 07:31
  • Something that doesn't make sense to me: You say you use JUnit 3 but the error message above says `org.junit_4.11.0`. Did you mix facts? – Aaron Digulla May 20 '14 at 07:35
  • 1
    @AaronDiguilla I assume you meant: Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and trstricted API -> Forbidden reference (access rules): -> change to warning. That changes the messages to warning, but the problem, the warnings themselves doesn't go away. – blazonix May 20 '14 at 07:40
  • @AaronDiguilla Oh no, the class above uses JUnit 4, but I also used JUnit 3 earlier, so both libraries are included. – blazonix May 20 '14 at 07:42
  • Okay, now I'm worried. When you look at the image, you can see that Eclipse adds the `junit.jar` from `org.junit_4.11.0` to your build when you select JUnit 3. Is that a clean install? – Aaron Digulla May 20 '14 at 07:43
  • Okay, I see the same behavior here. That's odd. – Aaron Digulla May 20 '14 at 07:46
  • Re Compiler settings: No, I wanted you to reset all compiler settings to the default. My guess is that there is another setting which you or someone changed which causes this. Having JUnit twice on the classpath shouldn't be a problem. – Aaron Digulla May 20 '14 at 07:51
  • @AaronDiguilla Actually, I've solved it - I removed JUnit 3. Maybe it's a clash in build paths or some where along this line of reasoning? – blazonix May 20 '14 at 07:51
  • @AaronDiguilla My compiler settings didn't change even when I clicked restore defaults - I don't think that was it. – blazonix May 20 '14 at 07:52
  • So the error is gone when you just have one JUnit on the classpath? I don't see how that would help :-/ – Aaron Digulla May 20 '14 at 08:31
  • @AaronDiguilla Me neither, but when I removed it, it worked, so... (/o _ o\) – blazonix May 20 '14 at 08:32
0

I had the same issue and could resolve it by removing junit from the manifest.mf files (using the dependency tab) in all my interacting plugin projects and then adding junit with the alternative aproach
Built Path ... --> Add Library... --> JUnit as already described in the previous post by Aaron.

Stefan
  • 10,010
  • 7
  • 61
  • 117
0

I ran into a very similar issue, and I was able to resolve it by updating an entry in my Eclipse project's classpath file that was pointing to an older version of JUnit.

I changed

<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>

to

<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
jstricker
  • 2,132
  • 1
  • 30
  • 44
0

This is probably an obvious but if you're using spring framework make sure you add the spring library to the build path as well.

Download spring library such as spring-framework-4.1.6.RELEASE-dist.zip - unzip to a folder on your laptop - C:\springLib\

Then add a User Library to the build path and point to this lib

mangel
  • 65
  • 1
  • 13