【问题标题】:JUnit throws java.lang.NoClassDefFoundError: org/hamcrest/MatcherAssertJUnit 抛出 java.lang.NoClassDefFoundError: org/hamcrest/MatcherAssert
【发布时间】:2016-01-29 08:20:09
【问题描述】:

我正在使用具有以下配置的 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))

【问题讨论】:

    标签: java maven exception junit hamcrest


    【解决方案1】:

    您应该使用hamcrest-library 而不是hamcrest-allhamcrest-all 不打算与依赖管理器一起使用,因为它是一个包含 hamcrest-corehamcrest-library 的 uber-jar。以下 sn-p 应该可以工作。

    <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>
    

    【讨论】:

      【解决方案2】:

      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>
      

      【讨论】:

        猜你喜欢
        • 2013-01-10
        • 1970-01-01
        • 2013-07-09
        • 1970-01-01
        • 1970-01-01
        • 2016-11-21
        • 2021-05-30
        相关资源
        最近更新 更多