首页 > 其他分享 >【单元测试】jUnit框架

【单元测试】jUnit框架

时间:2022-12-31 23:33:59浏览次数:38  
标签:RunWith 框架 单元测试 junit annotation 测试 Test jUnit JUnit

POM配置 

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

Java Test类结构

Class XXXTest{
@Test
Public void should_预期_when_条件()
// should_return 3_when_input_xxx_and_xxx
{
    Assert.assertXXX;
}
}

基础注解

  

 

   用例执行顺序 @FixMethodOrder,取值JVM(代码定义顺序)、NAME_ASCEDING(方法字母顺序)、DEFAULT(不可预期顺序)

  

基础断言

  

 

 高级断言 AssertThat

  

 

 异常测试

  @Test(expected = NullPointerException.class)

测试套

  

 

其他注解:

  @Ignore:忽略某些用例测试(一般要写上注释原因)

  @Test(timeout = 2)       单位毫秒,测试执行效率,超时参数,适用于单个方法

  

 

 参数化测试 @RunWith(Parameterized.class)

  

 

   场景:使用不同的值反复运行相同的测试

      1、@RunWith(Parameterized.class) 注释测试类

      2、创建使用@Parameter的公共静态方法,返回集合作为测试数据集

      3、创建构造函数或使用@Parameter来接收测试数据内容  

  

 

 Junit5 vs Junit4

  参考:https://www.baeldung.com/junit-5-migration

  • 注解变化 Before/After -> xxEach/xxAll

    

 

  •  The most important one is that we can no longer use @Test annotation for specifying expectations.

   

 

  •  We can now write assertion messages in a lambda in JUnit 5, allowing the lazy evaluation to skip complex message construction until needed:

    

 

  •  @RunWith -> @ExtendWith

    In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation. However, the @RunWith annotation can still be used in JUnit 5 for the sake of   

    backward compatibility.

    

标签:RunWith,框架,单元测试,junit,annotation,测试,Test,jUnit,JUnit
From: https://www.cnblogs.com/clarino/p/17017577.html

相关文章