问题描述
如果我导入org.junit.Test
,那么在运行JUnit时,它会显示错误消息"Test Runner JUnit 5未找到测试".
If I import org.junit.Test
then on running JUnit, it gives me an error as "No Test found with Test Runner JUnit 5".
相反,如果我导入org.junit.jupiter.api.Test
,那么我可以运行JUnit测试.
Instead if I import org.junit.jupiter.api.Test
then I can run JUnit test.
推荐答案
此org.junit.Test
是JUnit 4批注. Junit5测试运行程序不会发现带有org.junit.Test
注释的测试.
This org.junit.Test
is a JUnit 4 annotation. A Junit5 test runner will not discover a test annotated with org.junit.Test
.
Junit5测试运行器将发现带有org.junit.jupiter.api.Test
注释的测试.
A Junit5 test runner will discover a test annotated with org.junit.jupiter.api.Test
.
因此,这解释了为什么在尝试运行不包含任何带有org.junit.jupiter.api.Test
注释的测试的测试上下文时,得到"使用Test Runner JUnit 5未找到测试"的原因.
So, this explains why you get "No Test found with Test Runner JUnit 5" when attempting to run a test context which does not contain any tests annotated with org.junit.jupiter.api.Test
.
听起来像,您可能正在从Junit4迁移到Junit5.如果是这样,您将需要进行一些更改. Junit5文档提供了一些有用的技巧,包括:
It sounds like you might be migrating from Junit4 to Junit5. If so, there are several changes you'll need to come to terms with. The Junit5 docs offer some useful tips including:
断言驻留在org.junit.jupiter.api.Assertions
中.
假设位于org.junit.jupiter.api.Assumptions
.
@Before
和@After
不再存在;使用@BeforeEach
和@AfterEach
代替.
@Before
and @After
no longer exist; use @BeforeEach
and @AfterEach
instead.
@BeforeClass
和@AfterClass
不再存在;使用@BeforeAll
并 @AfterAll
代替.
@BeforeClass
and @AfterClass
no longer exist; use @BeforeAll
and @AfterAll
instead.
@Ignore
不再存在:使用@Disabled
或其他内置项之一 代替执行条件
@Ignore
no longer exists: use @Disabled
or one of the other built-in execution conditions instead
@Category
不再存在;使用@Tag
代替.
@Category
no longer exists; use @Tag
instead.
@RunWith
不再存在;被@ExtendWith
取代.
@Rule
和@ClassRule
不再存在;被@ExtendWith
取代,并 @RegisterExtension
@Rule
and @ClassRule
no longer exist; superseded by @ExtendWith
and @RegisterExtension
这篇关于导入org.junit.Test会引发错误,因为“未通过测试运行器"JUnit 5"找到测试".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!