无法导入 org.junit.Assert.AssertThat;

2022-09-02 13:03:23

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


答案 1

静态导入

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

import static org.junit.Assert.assertThat

现在在客户端代码中你可以做assertThat(something, ismatched())

参考:Java 教程 > 静态导入语句


常规进口

要以老式的方式做到这一点,如果你像这样导入类Assert

import org.junit.Assert

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

(该方法是您必须实现的东西)isMatched()


assertThat()

assertThat()首次在这篇博客文章中进行了描述,并且自 4.4 版以来一直是 JUnit 的一部分,因此请确保类路径上有 JUnit 4.4 版或更高版本。此外,请确保您的编译器合规性级别为 1.5 或更高版本:

Set Eclipse compiler compliance level


答案 2

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

import static org.junit.Assert.assertThat;

但是,由于您没有告诉我们错误消息,因此我无法真正判断这是否适合您。