【问题标题】:How do I use Hamcrests contains method with JUnit 5?如何在 JUnit 5 中使用 Hamcrests contains 方法?
【发布时间】:2018-10-03 11:07:39
【问题描述】:

Screenshot of Java code in Eclipse

JUnit 5 似乎不包含以下 Hamcrest 导入所需的 JAR:

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.containsInAnyOrder;

当我添加 hamcrest-all-1.3.jar(见屏幕截图)时,它起作用了,但是在我运行测试时它导致了错误。

我认为编译器不喜欢 Junit 中的核心 jar 和我的 JAR 中都有重复项。

请帮忙,谢谢!

【问题讨论】:

  • 第三行语法不正确。删除static 并将末尾的. 替换为;。然后导入语句应该编译,其余的应该自己解决。如果没有,请在您的问题中包含有关导入的错误消息。

标签: java junit4 hamcrest junit5


【解决方案1】:

Hamcrest 1.3 应该与 JUnit 5.1 一起使用,很可能您混淆了依赖项。如果您的类路径与其他依赖项发生冲突,您可能需要使用 hamcrest-corehamcrest-library

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-api</artifactId>
  <version>5.1.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.1.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <version>5.1.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.platform</groupId>
  <artifactId>junit-platform-launcher</artifactId>
  <version>1.1.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.platform</groupId>
  <artifactId>junit-platform-runner</artifactId>
  <version>1.1.0</version>
  <scope>test</scope>
</dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-23
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2019-09-22
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多