Junit常用注解

版权声明:欢迎转载,请注明作者和出处 https://blog.csdn.net/baichoufei90/article/details/84336705

Junit常用注解

0x01 摘要

本文简要说下junit里面常用注解的含义和使用,还会总结一些常用的Assert判断语句。

0x02 常用Junit注解

2.1 @Test

用在方法上,定义该方法是测试方法。

注意:测试方法必须是public void,但可以抛异常,不过一般不这么做。

例子:

@Test (timeout = 30000)
  public void testRMAppSubmitInvalidResourceRequest() throws Exception {
    asContext.setResource(Resources.createResource(
        YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB + 1));

    // submit an app
    try {
      appMonitor.submitApplication(asContext, "test");
      Assert.fail("Application submission should fail because resource" +
          " request is invalid.");
    } catch (YarnException e) {
      // Exception is expected
      // TODO Change this to assert the expected exception type - post YARN-142
      // sub-task related to specialized exceptions.
      Assert.assertTrue("The thrown exception is not" +
          " InvalidResourceRequestException",
          e.getMessage().contains("Invalid resource request"));
    }
  }

2.2 @BeforeClass

2.2.1 含义

用来修饰所有@Test方法之前只会运行一次的方法。

注意:需用public static void 修饰。

2.2.2 用途

当你的测试中多个方法都会用到的一些公共方法可以抽取到用@BeforeClass修饰的方法汇总,如创建数据库驱动等。

2.2.3 示例

@BeforeClass
public static void loadDriver(){
    try {
        Class.forName("org.apache.phoenix.queryserver.client.Driver");
    } catch (ClassNotFoundException e) {
        logger.error("an exception happened while load driver:", e);
    }
}

2.3 @AfterClass

2.3.1 含义

@AfterClass和前面提到的@BeforeClass类似,但是在所有@Test方法之后运行一次。

注意:需用public static void 修饰。

2.3.2 用途

一般他被来处理一些资源清理工作,如关闭数据库连接等。

2.3.3 示例

@AfterClass
public static void closeConnections(){
    for(Connection conn : connectionList){
        try {
            conn.close();
        } catch (SQLException e) {
            logger.error("an exception happened while close connection:", e);
        }
    }
}

2.4 @Before

2.4.1 含义

@Before@BeforeClass类似,他们最大区别在于@Before会在每个@Test方法运行之前都会运行一次。

注意:必须用public void修饰,不能加static

2.4.2 用途

用于每个测试用例都需要的单独的方法,比如获取插入一条记录到数据。

2.4.3 示例

@Before
public void setUp() {
  long now = System.currentTimeMillis();

  rmContext = mockRMContext(1, now - 10);
  ResourceScheduler scheduler = mockResourceScheduler();
  Configuration conf = new Configuration();
  ApplicationMasterService masterService =
      new ApplicationMasterService(rmContext, scheduler);
  appMonitor = new TestRMAppManager(rmContext,
      new ClientToAMTokenSecretManagerInRM(), scheduler, masterService,
      new ApplicationACLsManager(conf), conf);

  appId = MockApps.newAppID(1);
  RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null);
  asContext =
      recordFactory.newRecordInstance(ApplicationSubmissionContext.class);
  asContext.setApplicationId(appId);
  asContext.setAMContainerSpec(mockContainerLaunchContext(recordFactory));
  asContext.setResource(mockResource());
  setupDispatcher(rmContext, conf);
}

2.5 @After

2.5.1 含义

@After@AfterClass类似,他们最大区别在于@After会在每个@Test方法运行之后都会运行一次。

注意:必须用public void修饰,不能加static

2.5.2 用途

可以用来清理每个@Test@Before创建的单独资源、恢复测试初始状态等。

2.5.3 示例

@After
public void tearDown() {
  setAppEventType(RMAppEventType.KILL);
  ((Service)rmContext.getDispatcher()).stop();
}

2.6 @Runwith

2.6.1 含义

首先要分清几个概念:测试方法、测试类、测试集、测试运行器:

  • 其中测试方法就是用@Test注解的一些函数
  • 测试类是包含一个或多个测试方法的一个Test.java文件
  • 测试集是一个suite,可能包含多个测试类
  • 测试运行器则决定了用什么方式偏好去运行这些测试集/类/方法

而@Runwith就是放在测试类名之前,用来确定这个类怎么运行的。也可以不标注,会使用默认运行器。

常见的运行器有:

  1. @RunWith(Parameterized.class) 参数化运行器,配合@Parameters使用junit的参数化功能
  2. @RunWith(Suite.class)
    @SuiteClasses({ATest.class,BTest.class,CTest.class})

测试集运行器配合使用测试集功能

  1. @RunWith(JUnit4.class)

junit4的默认运行器

  1. @RunWith(JUnit38ClassRunner.class)

用于兼容junit3.8的运行器

  1. 一些其它运行器具备更多功能。例如@RunWith(SpringJUnit4ClassRunner.class)集成了spring的一些功能

2.7 @Parameters

用于使用参数化功能。

0xFF 参考文档

junit常用注解详细说明

猜你喜欢

转载自blog.csdn.net/baichoufei90/article/details/84336705
今日推荐