Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InaccessibleObjectException when running tests

Tags:

java

junit

I created the first test for a project as below:

package my.module.sub;

import org.junit.jupiter.api.Test;

public class FirstTest {

  @Test
  void firstTest() {
    System.out.println("Tests are now running");
  }

}

But when I run the tests I get the below error:

java.lang.reflect.InaccessibleObjectException: Unable to make void my.module.sub.FirstTest.firstTest() accessible: module my.module does not "opens my.module.sub" to unnamed module @7dc0f706

It seems that if I add opens my.module.sub to the module-info.java file the test will run fine.

Obviously I would rather that not be there. So what is the best way to prevent this error from occurring?

like image 288
KNejad Avatar asked Sep 19 '25 05:09

KNejad


1 Answers

The issue turned out to be because firstTest() was not public. So when running the tests, JUnit was not able to access the method.

In the end all I had to do to fix the issue was change firstTest() to:

public void firstTest()
like image 89
KNejad Avatar answered Sep 20 '25 19:09

KNejad