在 TestClass 中找不到测试 你是不是忘记了 @Test 注解?

No tests found in TestClass Haven't you forgot @Test annotation?

我在 运行 我的测试时遇到这样的错误:

org.mockito.exceptions.base.MockitoException: 

No tests found in TestCase
Haven't you forgot @Test annotation?

我当然有一个用 @Test 注释的方法。我做错了什么?

方法需要显式声明为public:

@Test
public void asdf() {
    asdf...
}

导入 junit 包而不是 testng 包。

即使我有一个用 @Test 注释的 public 方法,我还是遇到了这个异常。原来是导入 org.junit.jupiter.api.Test,我改成 org.junit.Test,效果很好。

如果你已经升级到 Junit 5,根据这个 :

“没有规则,没有跑步者 JUnit 4 规则和运行器在 JUnit 5 中不起作用,因此不能使用 MockitoRule 和 Mockito 运行器。

改用@ExtendWith(MockitoExtension.class)。在 Junit 5.6.2 和 Spring boot 2.3.1:

上验证了这一点

@ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) public class ClassTest {}

也不要忘记将测试方法显式声明为 public。