0

I'm writing some Java code using pattern-matching switch, which requires --enable-preview to be set in several contexts (compiler, runtime, IDE). At this point, I've got all except one of them: unit tests.

I have these in pom.xml:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
      <source>18</source>
      <target>18</target>
      <compilerArgs>--enable-preview</compilerArgs>
    </configuration>
  </plugin>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.13.2</version>
  <scope>test</scope>
</dependency>

and Maven successfully enables preview for the compiler, but not the unit tests. How do I enable it for junit?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 1
    Have you tried configuring the surefire plugin? See suggestions in https://stackoverflow.com/questions/55303837/problem-running-tests-with-enabled-preview-features-in-surefire-and-failsafe and https://stackoverflow.com/questions/61044079/maven-surefire-plugin-not-using-enable-preview-mode – Lesiak Feb 22 '23 at 15:38

1 Answers1

0

First you should upgrade the plugin version to the most recent version 3.10.1 as well as for maven-surefire-plugin (3.0.0-M9):

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <compilerArgs>
          <compilerArg>--enable-preview</compilerArg>
        </compilerArgs>
      </configuration>
    </plugin>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>--enable-preview</argLine>
      </configuration>
    </plugin>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235