简体   繁体   English

使用Maven在eclipse中为JUnit 5配置类路径

[英]Configure classpath in eclipse for JUnit 5 with Maven

Eclipse (2018-09) supports JUnit 5 tests only if the JUnit 5 engine is present on the classpath of the project. 只有在项目的类路径中存在JUnit 5引擎时,Eclipse(2018-09)才支持JUnit 5测试。

Now I have two possibilities in my Maven project: 现在我在Maven项目中有两种可能性:

  1. Add it to the project via an eclipse JUnit Library 通过eclipse JUnit Library将其添加到项目中

    JUnitLib

    and add only the API to the dependencies 并仅将API添加到依赖项

     <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 将引擎和API添加到我的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. 如果我做前者,那么每个使用eclipse的人都必须自己做。

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. 如果我这样做,那么我用实现类污染我的(测试)编译时类路径,我(和其他IDE的用户)可能会使用而不是API类。 Also this might cause conflicts with the IDE that might need a different version of the engine, than the one on the cp. 此外,这可能会导致与可能需要不同版本引擎的IDE冲突,而不是与cp上的版本冲突。 IIRC that was the whole point why API and engine have been split up in the first place. IIRC,这就是为什么API和引擎首先被拆分的重点。

Unfortunately there is no testRuntimeOnly scope in Maven (like there is in gradle). 不幸的是,Maven中没有testRuntimeOnly范围(就像在gradle中一样)。

TLDR : Which way is the correct way to configure JUnit 5 for eclipse for a Maven project? TLDR :对于Maven项目,为eclipse配置JUnit 5的正确方法是什么?

If I do the former, then everybody using eclipse has to do this himself. 如果我做前者,那么每个使用eclipse的人都必须自己做。

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 . 我假设您打算通过右键单击JUnit测试类并选择Run as> JUnit Test来为eclipse用户提供执行测试的机会。 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 . 我不知道这是否是正确的方法,但除此之外,您需要添加除JUnit Jupiter API / Engine之外的其他依赖项,即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. 不确定它是否相关,但我正在使用maven- surefire -plugin版本2.22.1,如果缺少JUnit Platform Launcher,则会抛出ClassNotFoundException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM