How to use junit5 ExtendWith with modules when -Werror is present

Viewed 425

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?

1 Answers

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.

Related