title: How do I open packages and require dependencies on test scope modules only for JUnit testing
body: <p>I'm migrating a jar project from java 10 using classpath to java 11 using the java 9 jigsaw modules.
  There are JUnit5 tests for the project.
  The test dependencies are provided at test scope by maven.
  How to make all packages open for testing but not open when the module is used by another project?</p>
  
  <p>The jar project is just providing a few classes (like a utility project) for other projects (so no main class needed).</p>
  
  <p>The project got 5 packages at <em>/src/main/java/a/b/c/</em>.
  2 of them should be accessible for projects using this jar.
  The other 3 are for internal use only (used by the accessible ones).
  The tests are located at <em>/src/test/java/a/b/c/</em>.
  These tests have dependencies (JUnit,  mockito, junt-params)  provided in test scope, since the tests are not relevant for the projects using this jar</p>
  
  <pre><code>&lt;dependency&gt;
  &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
  &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
  &lt;version&gt;${junit.version}&lt;/version&gt;
  &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I have provided a <strong>module-info.java</strong> at <em>/src/main/java/</em> :</p>
  
  <pre><code>module moduleName {
  
  requires java.base;
  
  exports a.b.c.package1;
  exports a.b.c.package3;
  }
  </code></pre>
  
  <p>so now the public classes in package 1 and 3 should be accessible to other projects as expected (I have not been able to verify this yet).</p>
  
  <p>Running the Tests now results in java.lang.reflect.InaccessibleObjectException: Unable to make a.b.c.package1.collections.SomeTest() accessible: module moduleName does not "opens a.b.c.package1" to unnamed module @6a84a97d</p>
  
  <p>when I open the packages (with <strong>opens</strong>) everything runs smooth.
  But now all packages are now opened.
  I expect package 2, 4 and 5 to be only accessible while testing and 1 and 3 should not be open for reflection (so only exported).</p>
  
  <p>I came to think, since maven tells me <em>unnamed module</em> <strong>@6a84a97d</strong>, this might be a module created for testing.
  That made me try adding a <strong>module-info.java</strong> at <em>/src/test/java/</em> for testing.</p>
  
  <pre><code>module moduleNameTest {
  requires moduleName; // the code to test
  requires java.base;  // java.base could be transient on moduleName
  
  // test dependencies
  requires org.junit.jupiter.api;
  requires org.junit.jupiter.params;
  }
  </code></pre>
  
  <p>now maven (3.5.4) states:</p>
  
  <pre><code>src/test/java/module-info.java:[3,20] module not found: moduleName
  src/test/java/module-info.java:[4,31] module not found: org.junit.jupiter.api
  src/test/java/module-info.java:[5,31] module not found: org.junit.jupiter.params
  </code></pre>
  
  <p>Technologies:</p>
  
  <ul>
  <li>java openJDK 11</li>
  <li>mvn 3.5.4</li>
  <li>JUnit 5.3.0</li>
  <li>Surefire Plugin 3.0.0-M3</li>
  <li>mockito 2.21.0</li>
  </ul>
  
  <p>As stated earlier I expect package 2, 4 and 5 to be only accessible while testing and the tests to be run on building the jar with maven. Package 1 and 3 should be exported for use in other projects but not open for reflection (so only exported not opened).</p>
  
  <p>If you need additional information don't hesitate to ask.</p>
  
  <p>Thanks in Advance</p>
  
  <p>Kevin</p>