164.md 3.3 KB
Newer Older
W
wizardforcel 已提交
1
# JUnit5 `@AfterAll`注解示例
W
wizardforcel 已提交
2 3 4

> 原文: [https://howtodoinjava.com/junit5/after-all-annotation-example/](https://howtodoinjava.com/junit5/after-all-annotation-example/)

W
wizardforcel 已提交
5
JUnit5 [`@AfterAll`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/AfterAll.html) 注解替换了 JUnit4 中的`@AfterClass`注解。它用于表示应在当前测试类中的所有测试之后执行**注解方法**
W
wizardforcel 已提交
6

W
wizardforcel 已提交
7
## `@AfterAll`注解用法
W
wizardforcel 已提交
8

W
wizardforcel 已提交
9
使用`@AfterAll`注解方法,如下所示:
W
wizardforcel 已提交
10 11 12 13 14 15 16 17 18

```java
@AfterAll
public static void cleanUp(){
	System.out.println("After All cleanUp() method called");
}

```

W
wizardforcel 已提交
19
带注解的`@AfterAll`方法**必须是静态方法**,否则它将引发运行时错误。
W
wizardforcel 已提交
20 21 22 23 24 25 26 27 28 29 30

```java
org.junit.platform.commons.JUnitException: @AfterAll method 'public void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.cleanUp()' must be static.
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:66)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findAfterAllMethods$1(LifecycleMethodUtils.java:48)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterAllMethods(LifecycleMethodUtils.java:48)

```

W
wizardforcel 已提交
31
## `@AfterAll`注解示例
W
wizardforcel 已提交
32

W
wizardforcel 已提交
33
让我们举个例子。 我使用了一个`Calculator`类并添加了一个`add`方法。 我将使用`@RepeatedTest`注解对其进行 5 次测试。 此注解将导致`add`测试运行 5 次。 但是`@AfterAll`带注解的方法只能调用一次。
W
wizardforcel 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

```java
package com.howtodoinjava.junit5.examples;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class AfterAnnotationsTest {

	@DisplayName("Add operation test")
	@RepeatedTest(5)
	void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) 
	{
		System.out.println("Running test -> " + repetitionInfo.getCurrentRepetition());
		Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");
	}

	@AfterAll
	public static void cleanUp(){
		System.out.println("After All cleanUp() method called");
	}

	@AfterEach
	public void cleanUpEach(){
		System.out.println("After Each cleanUpEach() method called");
	}
}

```

W
wizardforcel 已提交
73
其中`Calculator`类是:
W
wizardforcel 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

```java
package com.howtodoinjava.junit5.examples;

public class Calculator 
{
	public int add(int a, int b) {
		return a + b;
	}
}

```

现在执行测试,您将看到以下控制台输出:

```java
Running test -> 1
After Each cleanUpEach() method called

Running test -> 2
After Each cleanUpEach() method called

Running test -> 3
After Each cleanUpEach() method called

Running test -> 4
After Each cleanUpEach() method called

Running test -> 5
After Each cleanUpEach() method called

After All cleanUp() method called

```

W
wizardforcel 已提交
109
显然,带注解的`@AfterAll`方法`cleanUp()`仅被调用一次。
W
wizardforcel 已提交
110 111 112

学习愉快!

W
wizardforcel 已提交
113
[源码下载](https://github.com/lokeshgupta1981/Junit5Examples/tree/master/JUnit5Examples)