簡體   English   中英

JUnit拋出java.lang.NoClassDefFoundError:org / hamcrest / MatcherAssert

[英]JUnit throws java.lang.NoClassDefFoundError: org/hamcrest/MatcherAssert

我正在使用具有以下配置的JUnit 4.11Hamcrest 1.1編碼測試:

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

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.1</version>
  <scope>test</scope>
</dependency>

當我運行它們時,出現以下異常:

java.lang.NoClassDefFoundError: org/hamcrest/MatcherAssert

在包含以下內容的行中:

assertThat(obj, hasProperty('id', 1L))

Junit對org.hamcrest:hamcrest-core:1.3:compile有自己的依賴性。

通過將依賴項更改為解決了問題:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>

您應該使用hamcrest-library而不是hamcrest-all hamcrest-all不能與依賴項管理器一起使用,因為它是包含hamcrest-corehamcrest-library的uber-jar。 以下代碼段應該起作用。

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

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-library</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM