本文介绍了导入org.junit.Test会引发错误,因为“未通过测试运行器"JUnit 5"找到测试".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我导入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"找到测试".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:38
查看更多