Why upgrade
The characteristics and advantages of upgrade to junit5 . One of my favorite points is that Junit 4 misses many features of Java 8. Junit5 can make good use of the characteristics of , and the expression of 161455f17dd129 code is more clear and concise.
The following upgrade methods are compatible with the original single test of junit4 (there is no need to change the inventory single test), and can use the new features of junit5, isn't it cool!
The specific steps of the upgrade
Introduce dependencies in pom
<dependencyManagement>
<dependencies>
<!-- 引入junit5的bom -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.5.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- 用于兼容Junit4和junit3 -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
New entry of junit5 single test application
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("ut")
@Transactional
public class ApplicationJunit5Test {
}
The newly added junit5 single test can inherit MochaApplicationJunit5Test
Abnormal retry function
In the single test execution, the most intuitive idea is to retry when encountering the abnormal database deadlock. Junit5 supports abnormal retry. The specific operations are as follows
Introduce rerunner-jupiter
<dependencies>
<dependency>
<groupId>io.github.artsok</groupId>
<artifactId>rerunner-jupiter</artifactId>
<version>2.1.6</version>
<scope>test</scope>
</dependency>
</dependencies>
Add retry annotations for specific single tests
For each user center retry example, 3 retries were performed for MySQLTransactionRollbackException. To unlock more skills, please move to rerunner-jupiter document
Note, do not repeat the @Test annotation on the
@RepeatedIfExceptionsTest(repeats = 3,
exceptions = MySQLTransactionRollbackException.class,
name = "Retry deadlock failed test. Attempt {currentRepetition} of {totalRepetitions}")
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。