blazonix
blazonix

Reputation: 433

Cause of this error: The type Test is not accessible due to restriction on required library?

**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.

Upvotes: 0

Views: 18536

Answers (4)

mangel
mangel

Reputation: 65

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

Upvotes: 0

jstricker
jstricker

Reputation: 2180

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"/>

Upvotes: 0

Stefan
Stefan

Reputation: 12360

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.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328754

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.

Upvotes: 1

Related Questions