Configure classpath in eclipse for JUnit 5 with Maven

ST-DDT :

Eclipse (2018-09) supports JUnit 5 tests only if the JUnit 5 engine is present on the classpath of the project.

Now I have two possibilities in my Maven project:

  1. Add it to the project via an eclipse JUnit Library

    JUnitLib

    and add only the API to the dependencies

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    
  2. Add both the engine and the API to to my Maven pom

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>
    

If I do the former, then everybody using eclipse has to do this himself.

If I do the later, then I pollute my (test) compile time classpath with implementation classes, that I (and users of other IDEs) might use instead of the API classes. Also this might cause conflicts with the IDE that might need a different version of the engine, than the one on the cp. IIRC that was the whole point why API and engine have been split up in the first place.

Unfortunately there is no testRuntimeOnly scope in Maven (like there is in gradle).

TLDR: Which way is the correct way to configure JUnit 5 for eclipse for a Maven project?

Tobias Roloff :

If I do the former, then everybody using eclipse has to do this himself.

I assume you intended to give an eclipse user a chance to execute a test by right clicking a JUnit test class and selecting Run as > JUnit Test. I don't know if this is the right way but to do so you need to add an additional dependency besides JUnit Jupiter API/Engine, which is JUnit Platform Launcher.

For example:

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-api</artifactId>
  <version>5.4.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.4.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.platform</groupId>
  <artifactId>junit-platform-launcher</artifactId>
  <version>1.4.2</version>
  <scope>test</scope>
</dependency>

Not sure if its related but I am using maven-surefire-plugin Version 2.22.1 which throws ClassNotFoundException if JUnit Platform Launcher is missing.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103951&siteId=1