Junit4升级至Junit5

为什么要升级

junit5的特性和优点可以参考升级到junit5。其中我最看中的点是,Junit4错过了好多Java8的好多特性。Junit5可以很好得利用java8的特性,代码的表达更加清晰和简洁。

以下升级方式即能兼容原来junit4的单测(不需要改存量单测),又能使用junit5的新特性,是不是很爽!

升级的具体步骤

pom中引入依赖


    
                
        
            org.junit
            junit-bom
            5.5.2
            pom
            import
        
    



    
    
        org.junit.jupiter
        junit-jupiter
        test
    


    
    
        org.junit.vintage
        junit-vintage-engine
        test
    

异常重试功能

在单测执行中,遇到数据库死锁的异常,最直观的想法是进行重试,junit5支持异常重试,具体操作如下

引入rerunner-jupiter

    
    
        io.github.artsok
        rerunner-jupiter
        2.1.6
        test
    

针对具体单测添加重试注解

各一个用户中心重试的例子,针对 MySQLTransactionRollbackException 异常进行了3次重试。更多技能解锁,请移步rerunner-jupiter文档

@RepeatedIfExceptionsTest(repeats = 3,
        exceptions = MySQLTransactionRollbackException.class,
        name = "Retry deadlock failed test. Attempt {currentRepetition} of {totalRepetitions}")

你可能感兴趣的:(Junit4升级至Junit5)