【问题标题】:Unable to import org.junit.Assert.AssertThat;无法导入 org.junit.Assert.AssertThat;
【发布时间】:2011-06-23 10:25:58
【问题描述】:

我无法在我的程序中import org.junit.Assert.AssertThat。我正在使用 Ganymede 和 jUnit 4.8.1。

【问题讨论】:

    标签: java eclipse junit import


    【解决方案1】:

    静态导入

    它是org.junit.Assert.assertThat(T, Matcher<T>),您可以将其作为静态导入导入:

    import static org.junit.Assert.assertThat
    

    现在您可以在您的客户端代码中执行assertThat(something, ismatched())

    参考:Java Tutorial > The Static Import Statement


    常规导入

    如果您像这样导入 Assert 类,请按照老派的方式进行操作

    import org.junit.Assert
    

    您可以使用Assert.assertThat(something, isMatched()) 调用它

    isMatched() 方法是你必须实现的)


    assertThat()

    assertThat() 最初被描述为 in this blog post 并且已经成为 JUnit ever since version 4.4 的一部分,因此请确保在类路径中具有 JUnit 4.4 或更新版本。此外,请确保您的编译器合规级别为 1.5 或更高:

    【讨论】:

      【解决方案2】:

      该方法称为assertThat(小写a,大写T)。如果你像这样导入它,你需要使用静态导入:

      import static org.junit.Assert.assertThat;
      

      但是由于您没有告诉我们错误消息,我无法确定这是否适合您。

      【讨论】:

        【解决方案3】:

        假设“我正在使用 ganymede”,您是在声明您正在使用“eclipse 的 ganymede 版本”,请执行以下操作:

        1. 打开项目属性。
        2. 点击“Java 构建路径”。
        3. 选择库选项卡。
        4. 点击“添加库”按钮。
        5. 选择junit。

        您现在应该可以将 junit 类导入您的项目。

        【讨论】:

        • 假设“我正在使用 ganymede” alternatives 听起来不太可能 :-)
        猜你喜欢
        • 1970-01-01
        • 2021-11-20
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        相关资源
        最近更新 更多