4

I am doing junit testing for my project that involves javafx and I encountered this issue:

Exception in thread "main" java.lang.IllegalAccessError: class org.junit.platform.engine.UniqueId (in unnamed module @0x67424e82) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module @0x67424e82
at org.junit.platform.engine.UniqueId.forEngine(UniqueId.java:67)
at com.intellij.junit5.JUnit5IdeaTestRunner.<clinit>(JUnit5IdeaTestRunner.java:86)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
at com.intellij.rt.junit.JUnitStarter.getAgentClass(JUnitStarter.java:244)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:225)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) Process finished with exit code 1

I keep searching up on StackOverflow and found something similar to the issue I'm having but it only deals with illegal access to main methods instead of junit testing: Two java files. Getting IllegalAccessError when running class with main method trying to access a method from the other file.

This is my pom.xml file

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>proj4</artifactId>
<version>1.0-SNAPSHOT</version>
<name>proj4</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>5.7.1</junit.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>17-ea+11</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>17-ea+11</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.6</version>
            <executions>
                <execution>
                    <!-- Default configuration for running with: mvn clean javafx:run -->
                    <id>default-cli</id>
                    <configuration>
                        <mainClass>com.example.proj4/application.proj4.PizzeriaApplication</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This is my test class

package application.proj4;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;

class PizzaTest {
    @Test
    void price()
    {
        
        ArrayList<Topping> n1 = new ArrayList<Topping>();
        Deluxe d1 = new Deluxe(Size.Small,n1);
        assertEquals(12.99, d1);
    } }
Josh S.
  • 101
  • 1
  • 6
  • The module system is restricting access. Study the [module system](https://www.oracle.com/corporate/features/understanding-java-9-modules.html) if you wish to learn more about it. – jewelsea Nov 14 '21 at 02:31
  • I think there is related info in this closred [junit issue](https://github.com/junit-team/junit5/issues/2147) which links to a [blog post on modular testing](https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world#module-infojavatest), which suggests some fixes, one of which is creating a separate module-info.java in the test source which allows access to the junit code. – jewelsea Nov 14 '21 at 02:48
  • @jewelsea I think I was able to pinpoint it to this issue: Module 'com.example.proj4' reads package 'javafx.beans' from both 'javafx.base' and 'javafx.base' as the file that has it had a red squiggly underline. I'll try to see if stackoverflow has anything more on that. – Josh S. Nov 14 '21 at 04:15
  • @jewelsea, I deleted module-info.java, then junit testing works. However, my program which is supposed to show a pizzeria interface doesn't work. How can I solve this? – Josh S. Nov 14 '21 at 04:38
  • This is not normally a recommended solution, the recommended solution is to create a test module info file as previously outlined, but, if you just don’t want to deal with the module system, remove all the module info files, run everything off the class path and use this [launcher class hack](https://stackoverflow.com/a/67668560/1155209). Perhaps also downgrade your junit version from 5 to 4 which likely isn’t modular. – jewelsea Nov 14 '21 at 04:51

0 Answers0