导入 org.junit.Test 作为 "No Test found with Test Runner "JUnit 5"" 抛出错误

Import org.junit.Test throws error as "No Test found with Test Runner "JUnit 5""

如果我导入 org.junit.Test 然后在 运行ning JUnit 上,它给我一个错误 "No Test found with Test Runner JUnit 5"。

相反,如果我导入 org.junit.jupiter.api.Test,那么我可以 运行 JUnit 测试。

org.junit.Test 是一个 JUnit 4 注释。 Junit5 测试 运行ner 不会 发现用 org.junit.Test.

注释的测试

Junit5 测试 运行ner 发现用 org.junit.jupiter.api.Test.

注释的测试

因此,这解释了为什么在尝试 运行 不包含任何带注释的测试的测试上下文时出现“未使用 Test Runner JUnit 5 找到测试org.junit.jupiter.api.Test.

听起来您可能正在从 Junit4 迁移到 Junit5。如果是这样,您需要接受一些更改。 Junit5 docs 提供了一些有用的提示,包括:

Annotations reside in the org.junit.jupiter.api package.

Assertions reside in org.junit.jupiter.api.Assertions.

Assumptions reside in org.junit.jupiter.api.Assumptions.

@Before and @After no longer exist; use @BeforeEach and @AfterEach instead.

@BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.

@Ignore no longer exists: use @Disabled or one of the other built-in execution conditions instead

@Category no longer exists; use @Tag instead.

@RunWith no longer exists; superseded by @ExtendWith.

@Rule and @ClassRule no longer exist; superseded by @ExtendWith and @RegisterExtension