2

There is a src/main/java/module-info.java with only an exports clause.

There is a src/test/java/example/sometest/AdderTest which which has:

package example.sometest;
public class AdderTest {
    @Test
    void test() {
        assertEquals(5, Adder.add(2, 3));
    }
}

So far so good. Now I use the @ExtendWith annotation.

package example.sometest;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith(NoopTestWatcher.class)
public class AdderTest { ... }

where

package example.sometest;
import org.junit.jupiter.api.extension.TestWatcher;
public final class NoopTestWatcher implements TestWatcher { }

And now mvn test gives errors

[WARNING] .../AdderTest.java:[9,2] class org.junit.jupiter.api.extension.ExtendWith in module is not exported

[WARNING] /.../NoopTestWatcher.java:[6,41] interface org.junit.jupiter.api.extension.TestWatcher in module is not exported

[ERROR] .../AdderTest.java: warnings found and -Werror specified

How to make ExtendWith work?

Naman
  • 27,789
  • 26
  • 218
  • 353
snaran
  • 163
  • 8
  • *`-Werror` specified*, are you treating warnings as errors? might be worth updating the title to add words "without warnings" – Naman Nov 12 '20 at 09:58
  • Where is `NoopTestWatcher` comming from? – khmarbaise Nov 12 '20 at 13:06
  • @Naman good idea done – snaran Nov 12 '20 at 16:57
  • @khmarbaise I put in the code for that class. It's my own class defined in the same package. – snaran Nov 12 '20 at 18:59
  • Do you have a link on github or alike of that project... with all code ... Which version of JUnit Jupiter do you use? How does your `module-info.java` look like? Which Java version do you use? – khmarbaise Nov 12 '20 at 19:18
  • Please see https://github.com/SiemelNaran/MavenProject @khmarbaise – snaran Nov 12 '20 at 20:04
  • 1
    @snaran see what? the 404? – Eugene Nov 13 '20 at 02:46
  • @snaran Also seeing 404... – khmarbaise Nov 13 '20 at 16:23
  • Sorry everyone, repository was private. Just made it public. – snaran Nov 13 '20 at 20:21
  • if you do not tag us by name with `@` - we have no idea you replied. but anyway you do understand what [this](https://github.com/SiemelNaran/MavenProject/blob/master/pom.xml#L53) does? – Eugene Nov 19 '20 at 04:19
  • -Werror makes the javac compile fail if there is any warning. It's good to have on as many warnings are symptoms of future design errors. @Eugene – snaran Nov 19 '20 at 19:38
  • @snaran but you have specified it and that is exactly why it fails – Eugene Nov 19 '20 at 19:42
  • @Eugene my goal is to make this standard pattern work. Here the JUnit library defines an annotation ExtendWith whose enum value is a user provided class, and as it is a reasonable thing to do it ought to work without compiler warnings if one chooses to use module-info.java. I know that I can turn off the type of warning like -Xlint:-exports but only when compiling the test classes, but it seems there ought to be a better way. Maybe there is a way like calling --add-exports for the javac test-compile phase, but I can't figure out what. – snaran Nov 20 '20 at 23:31

1 Answers1

0

I do not know how to say politely, but testing under JPMS and maven, is not that easy or good. You are hitting a problem of maven, imho gradle does it better, but this is the view I have from a limited perspective of the work that I have done and we mainly use gradle...

You can make this work, sort of. First you need a separate module-info.java in src/test/java (Intellij is not going to like that), then you need different package names in src/main/java and src/test/java (even the provided examples from the project owners do that, for example here). So your Interrupted would have to become public - isn't that beautiful?

Overall, you can start from surefire + JUnit5 tutorial, but as said above you will need to make a lot of changes for this to work. Changes that you might not like. You should also be aware that this problem is not new and testing in the modules work, is, in general, complicated.

Eugene
  • 117,005
  • 15
  • 201
  • 306