1

I'm trying to run all tests (packaged across many different subpackages) in a given (Java-16, modular) project, using the test suite feature of JUnit (4); but this is much harder than I'd like if to be, seemingly requiring an -add-opens JVM option per test (sub)package. Does a better solution exist?

A MVE would be a project mve with test suite

package mve.test;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import mve.test.sub.HelloTest;

@RunWith(Suite.class)
@Suite.SuiteClasses(HelloTest.class)
public class AllTests {
    // Empty by design.
}

and the (only) test in the subpackage mve.test.sub being

package mve.test.sub;

import org.junit.Test;

import mve.main.Hello;

public class HelloTest {
    @Test
    public void test() {
        Hello.main(null);
    }
}

The project exports nothing and requires nothing, which is how I'd like to keep it.

Is there any way to do this without adding --add-opens mve/mve.test.xxx=ALL-UNNAMED for each and every single subpackage xxx to the JVM?

If not, this is a heavy blow against using (a lot of) packages for your test classes.

Arend
  • 2,363
  • 1
  • 18
  • 18
  • 1
    You want the tests to be invoked by an external framework. Why do you refuse to export the test cases’ packages then? – Holger Oct 26 '22 at 08:06
  • 1
    Afaik, the maven patches the module under test with the test code, and opens all packages - at least last time I checked. – Johannes Kuhn Oct 26 '22 at 08:15
  • @Holger I don't event want to have my tests in my exported code. To my understanding, this is part of the whole concept of separating main and test code. This is supported in Eclipse (for instance) by the "Contains test sources" option. See also [Testing in a modular world](https://info.michael-simons.eu/2021/10/19/testing-in-a-modular-world/) – Arend Oct 26 '22 at 12:47
  • @JohannesKuhn Yes, Maven does that for you. However, that only helps if you run your tests from Maven, which doesn't give you the most user-friendly way to handle debugging in (say) Eclipse. I would prefer to be able to invoke the tests directly from Eclipse, hence my question – Arend Oct 26 '22 at 12:51
  • Apparently, the problem is that you have your test cases inside the same module as the production code. Just don’t do that. Put your test cases in a module of its own, then it can be open, without affecting the production code module. – Holger Oct 26 '22 at 15:18
  • 1
    By the way, did you read the article to the end, including the comment that says that you don’t need to specify `--add-opens` when you make the tests `public`? – Holger Oct 26 '22 at 15:29

0 Answers0