行业资讯 2025年08月6日
0 收藏 0 点赞 898 浏览 2272 个字
摘要 :

文章目录 1.JUnit RunListener示例 1.1 JUnit测试类 1.2 JUnit测试监听器 2. JUnit监听器执行 通常,监听器有助于监听我们所感兴趣的事件。这可能是出于多种原因。例……




通常,监听器有助于监听我们所感兴趣的事件。这可能是出于多种原因。例如,我们添加监听器以添加特定的日志,处理Java GUI编程中的UI事件等等。

JUnit还提供支持,通过RunListener类在执行测试时添加监听器。这种监听器可以用于各种目的,从改进日志记录到测试特定的逻辑。

1.JUnit RunListener示例

1.1 JUnit测试类

下面我们仅写两个测试类为例。我们将监视在这些类中编写的测试所打印的日志。

public class TestFeatureOne {
  @Test
  public void testFirstFeature()
  {
    Assert.assertTrue(true);
  }
}
public class TestFeatureTwo {
  @Test
  public void testSecondFeature()
  {
    Assert.assertTrue(true);
  }
 
  @Test
  @Ignore
  public void testSecondFeatureIngored()
  {
    Assert.assertTrue(true);
  }
}

1.2 JUnit测试监听器

让我们编写运行监听器。这个监听器将扩展JUnit提供的RunListener类。

public class ExecutionListener extends RunListener
{
  /**
   * 在运行任何测试之前调用的函数。
   * */
  public void testRunStarted(Description description) throws java.lang.Exception
  {
    System.out.println(\"Number of tests to execute : \" + description.testCount());
  }
 
  /**
   *  在所有测试完成后调用的函数。
   * */
  public void testRunFinished(Result result) throws java.lang.Exception
  {
    System.out.println(\"Number of tests executed : \" + result.getRunCount());
  }
 
  /**
   * 当原子测试即将开始时调用的函数。
   * */
  public void testStarted(Description description) throws java.lang.Exception
  {
    System.out.println(\"Starting execution of test case : \"+ description.getMethodName());
  }
 
  /**
   * 当原子测试完成时调用的函数,无论测试成功还是失败。
   * */
  public void testFinished(Description description) throws java.lang.Exception
  {
    System.out.println(\"Finished execution of test case : \"+ description.getMethodName());
  }
 
  /**
   *  原子试验失败时调用。
   * */
  public void testFailure(Failure failure) throws java.lang.Exception
  {
    System.out.println(\"Execution of test case failed : \"+ failure.getMessage());
  }
 
  /**
   * 当一个测试不会被运行时调用,通常是因为测试方法被标记为 Ignore。
   * */
  public void testIgnored(Description description) throws java.lang.Exception
  {
    System.out.println(\"Execution of test case ignored : \"+ description.getMethodName());
  }
}

我们可以自由地覆盖RunListener类中的任何数量的方法,包括根本不覆盖任何方法。

2. JUnit监听器执行

现在,让我们运行测试并观察监听器的输出。

public class ExecuteWithRunListener
{
  public static void main(String[] args)
  {
    JUnitCore runner = new JUnitCore();
    <strong>//Adding listener here</strong>
    runner.addListener(new ExecutionListener());
    runner.run(TestFeatureOne.class, TestFeatureTwo.class);
  }
}

程序输出:

Number of tests to execute : 3
 
Starting execution of test case : testFirstFeature
Finished execution of test case : testFirstFeature
 
Starting execution of test case : testSecondFeature
Finished execution of test case : testSecondFeature
 
Execution of test case ignored : testSecondFeatureIngored
 
Number of tests executed : 2

很明显,添加监听器为测试执行提供了额外的控制,同时改进了日志记录支持。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9537.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

988
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

465
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

348
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

456
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

517
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

832
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号