# JUnit 测试监听器– JUnit `RunListener`示例

> 原文： [https://howtodoinjava.com/junit/how-to-add-listner-in-junit-testcases/](https://howtodoinjava.com/junit/how-to-add-listner-in-junit-testcases/)

监听器通常可以帮助监听我们感兴趣的事件。 这可能有几个原因。 例如，我们添加了监听器以添加特定的日志，在 Java GUI 编程中处理 UI 事件等。

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

## 1\. JUnit `RunListener`示例

#### 1.1 JUnit 测试类

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

```java
package com.howtodoinjava.junit;

import junit.framework.Assert;

import org.junit.Test;

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

```

```java
package com.howtodoinjava.junit;

import junit.framework.Assert;

import org.junit.Ignore;
import org.junit.Test;

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

	@Test
	@Ignore
	public void testSecondFeatureIngored()
	{
		Assert.assertTrue(true);
	}
}

```

#### 1.2 JUnit 测试监听器

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

> 我们可以完全不包含任何方法来覆盖任何数量的方法`RunListener`类。

```java
package com.howtodoinjava.junit.suite;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

public class ExecutionListener extends RunListener
{
	/**
	 * Called before any tests have been run.
	 * */
	public void testRunStarted(Description description)	throws java.lang.Exception
	{
		System.out.println("Number of tests to execute : " + description.testCount());
	}

	/**
	 *  Called when all tests have finished
	 * */
	public void testRunFinished(Result result) throws java.lang.Exception
	{
		System.out.println("Number of tests executed : " + result.getRunCount());
	}

	/**
	 *  Called when an atomic test is about to be started.
	 * */
	public void testStarted(Description description) throws java.lang.Exception
	{
		System.out.println("Starting execution of test case : "+ description.getMethodName());
	}

	/**
	 *  Called when an atomic test has finished, whether the test succeeds or fails.
	 * */
	public void testFinished(Description description) throws java.lang.Exception
	{
		System.out.println("Finished execution of test case : "+ description.getMethodName());
	}

	/**
	 *  Called when an atomic test fails.
	 * */
	public void testFailure(Failure failure) throws java.lang.Exception
	{
		System.out.println("Execution of test case failed : "+ failure.getMessage());
	}

	/**
	 *  Called when a test will not be run, generally because a test method is annotated with Ignore.
	 * */
	public void testIgnored(Description description) throws java.lang.Exception
	{
		System.out.println("Execution of test case ignored : "+ description.getMethodName());
	}
}

```

## 2\. JUnit 监听器执行

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

```java
package com.howtodoinjava.junit.suite;

import org.junit.runner.JUnitCore;

import com.howtodoinjava.junit.TestFeatureOne;
import com.howtodoinjava.junit.TestFeatureTwo;

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);
	}
}

```

程序输出。

```java
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

```

显然，添加监听器可通过改进的日志记录支持对测试执行提供额外的控制。

学习愉快！