0

Assume these tools are used for running tests in a maven project:

  • org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5 with default configuration
  • org.junit.jupiter:junit-jupiter-engine:5.7.0

Please, find minimal example here.

The project consists of one java module (java 11, maven 3.6.1).

With this setup given a typical successful test looks like:

public class SampleTest {
  @Test
  public void test() {
    assertEquals(1, 1);
  }
}

I would like to avoid using public for all test methods and test classes, because it makes only visual noise. Unfortunately, after removing public visibility the test fails to start.

class SampleTest {
  @Test
  void test() {
    assertEquals(1, 1);
  }
}

mvn test

...
java.lang.reflect.InaccessibleObjectException: 
  Unable to make org.sample.SampleTest.test() accessible:
  module sample.core does not "opens org.sample" to unnamed module @7d9d1a19

Please, help me to find a proper way to configure the surefire plugin, so it could run tests without requiring public everywhere.

If needed, change version of surefire and junit - they are here just for clarity.

diziaq
  • 6,881
  • 16
  • 54
  • 96
  • You are mixing two things. Have a test class being public or not and using Java module system via `module-info.java`? These completely two different things cause java module system if it is not exported you can access to this... The question is here really want to test on java module base? Or do you like to have your test classes not being public (using JUnit Jupiter) this works perfectly... – khmarbaise Dec 09 '20 at 14:17
  • @khmarbaise Do you mean if a projects uses java modules, it is required to put "public" on all test classes? I believe it has nothing to deal with "exports", because in this example the module "org.sample" exports nothing and the test runs as expected if only it has "public" modifier. – diziaq Dec 09 '20 at 14:23
  • I have not understood why making test classes public is a problem. – J Fabian Meier Dec 09 '20 at 14:42
  • If you are using modules and testing with modules the JVM limits the access to the classes which are not public (does not allow them). The question is: Do you have a module-info.java in your `src/test/java` directory? – khmarbaise Dec 09 '20 at 14:57
  • @JFabianMeier It is not a problem, just an inconvenience. JUnit 5 allows omitting the "public" modifier, nevertheless it is still there in the code, When I see something in the code (i.e. visibility modifier) I expect that it tells me something about the project specifics. But here it does not, because it makes only noise and boilerplate. – diziaq Dec 09 '20 at 14:57
  • @JFabianMeier This is related to java modules are more restrictive than the classpath (no prevention of access via reflection)... modules violate that... that means a testing must be done either without modules or in complete different module.. (that's bit harder). – khmarbaise Dec 09 '20 at 14:58
  • Everywhere I use JUnit Jupiter my tests are not public neither the methods nor the classes... – khmarbaise Dec 09 '20 at 14:59
  • @khmarbaise No I have no module-info in the `test/java` directory. Unfortunately, I did not manage to find a concrete example, although there are some guides that mention that. I suppose this "test module" should have a different name from the one I have in `main/java/module-info.java`. Is this correct? – diziaq Dec 09 '20 at 15:00
  • I can recommend https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world.html No module-info in `src/test/java` can you make an example project on github or alike? – khmarbaise Dec 09 '20 at 15:15
  • @khmarbaise Thank you for the recommendation. Please, find the link to the example project in the first paragraph of the updated question. – diziaq Dec 10 '20 at 05:54
  • @diziaq The error looks like only as an exception message without a stack trace. Can you grab the stack trace from a dump file located in target/surefire-reports? We need to see more. Thx. – tibor17 Dec 13 '20 at 22:02
  • @tibor17 This question shows context and definition of the problem without extra noise. I suppose it would be much easier to clone the "minimal example project" and run `mvn test` to see the complete stdouput. – diziaq Dec 14 '20 at 13:24

1 Answers1

1

I made your test pass after:

  1. renamed the Java package com.sample to com.sample1 in src/test/java
  2. then added module-info.java in src/test/java

see the content of src/test/java/module-info.java

open module test.module {
    requires sample.module;
    requires transitive org.junit.jupiter.engine;
    requires transitive org.junit.jupiter.api;
}

I know that you want to have the same Java packages in both modules (src/main/java and src/test/java) but it won't be possible in JPMS, AFAIK. The keyword "open" is necessary since of JUnit 5.7 or something, and it would fail if you remove it:

module test.module does not "opens com.sample1" to module org.junit.platform.commons
diziaq
  • 6,881
  • 16
  • 54
  • 96
tibor17
  • 1,043
  • 6
  • 9