1、迁移小贴士
注解在org.junit.jupiter.api包;
断言使用org.junit.jupiter.api.Assertions类
org.junit.Assert和其他断言库(AssertJ, Hamcrest, Truth)依然可用
假设使用org.junit.jupiter.api.Assumptions类
| JUnit4 | JUnit5 |
|
@Before、@After
|
@BeforeEach、@AfterEach
|
|
@BeforeClass、@AfterClass
|
@BeforeAll、@AfterAll
|
|
@Ignore
|
@Disabled
|
|
@Category
|
@Tag
|
|
@RunWith
|
@ExtendWith
|
|
@Rule、@ClassRule
|
@ExtendWith、@RegisterExtension
|
2、JUnit4@Ignore支持
import org.junit.Ignore;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.EnableJUnit4MigrationSupport;
// @ExtendWith(IgnoreCondition.class)
@EnableJUnit4MigrationSupport
class IgnoredTestsDemo {
@Ignore
@Test
void testWillBeIgnored() {
}
@Test
void testWillBeExecuted() {
}
}
3、错误信息参数变更
JUnit4:assertEquals(String message, Object expected, Object actual)
JUnit5:assertEquals(Object expected, Object actual, String message).
Assertions
◦ assertTrue
◦ assertFalse
◦ assertNull
◦ assertNotNull
◦ assertEquals
◦ assertNotEquals
◦ assertArrayEquals
◦ assertSame
◦ assertNotSame
◦ assertThrows
• Assumptions
◦ assumeTrue
◦ assumeFalse

该文提供从JUnit4向JUnit5迁移的关键提示,包括使用org.junit.jupiter.api包中的新断言和假设方法,如Assertions和Assumptions类。@BeforeEach和@AfterEach替换@Before和@After,@BeforeAll和@AfterAll替换@BeforeClass和@AfterClass。同时,@Ignore在JUnit5中处理方式也有所变化,需使用@EnableJUnit4MigrationSupport。此外,还详细列出了JUnit5中各种断言和假设的方法。
8301

被折叠的 条评论
为什么被折叠?



