提交 41041887 编写于 作者: W wizardforcel

2020-06-05 11:54:33

上级 a56acb15
# JUnit 5 @BeforeAll 注释示例
# JUnit 5 `@BeforeAll`注释示例
> 原文: [https://howtodoinjava.com/junit5/before-all-annotation-example/](https://howtodoinjava.com/junit5/before-all-annotation-example/)
JUnit 5 [@BeforeAll](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/BeforeAll.html) 注释替换了 [JUnit 4](//howtodoinjava.com/junit-4/) 中的`@BeforeClass`注释。 它用于表示应在当前测试类中的所有测试之前执行**注释方法。**
JUnit 5 [`@BeforeAll`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/BeforeAll.html) 注释替换了 [JUnit 4](//howtodoinjava.com/junit-4/) 中的`@BeforeClass`注释。 它用于表示应在当前测试类中的所有测试之前执行**注释方法**
## @BeforeAll 注释用法
## `@BeforeAll`注释用法
使用`@BeforeAll`注释方法,如下所示:
......@@ -16,7 +16,7 @@ public static void init(){
```
带注释的`@BeforeAll` **方法必须是[静态](https://howtodoinjava.com/java/basics/java-static-keyword/)方法**,否则它将引发运行时错误。
`@BeforeAll`注释的方法**必须是[静态](https://howtodoinjava.com/java/basics/java-static-keyword/)方法**,否则它将引发运行时错误。
```java
org.junit.platform.commons.JUnitException: @BeforeAll method 'public void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.init()' must be static.
......@@ -27,7 +27,7 @@ at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080)
at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeAllMethods(LifecycleMethodUtils.java:42)
```
## @BeforeAll 注释示例
## `@BeforeAll`注释示例
让我们举个例子。 我使用了一个`Calculator`类并添加了一个`add`方法。 我将使用`@RepeatedTest`注解对其进行 5 次测试。 此注释将导致`add`测试运行 5 次。 但是`@BeforeAll`带注释的方法只能调用一次。
......@@ -62,7 +62,7 @@ public class BeforeAllTest {
```
其中 Calculator 类是:
其中`Calculator`类是:
```java
package com.howtodoinjava.junit5.examples;
......
# JUnit 5 @BeforeEach 注释示例
# JUnit 5 `@BeforeEach`注释示例
> 原文: [https://howtodoinjava.com/junit5/before-each-annotation-example/](https://howtodoinjava.com/junit5/before-each-annotation-example/)
JUnit 5 [@BeforeEach](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/BeforeEach.html) 注释替换了 JUnit 4 中的`@Before`注释。它用于表示应在当前类中的每个`@Test`方法之前执行**注释方法。 。**
JUnit 5 [`@BeforeEach`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/BeforeEach.html)注释替换了 JUnit 4 中的`@Before`注释。它用于表示应在当前类中的每个`@Test`方法之前执行**注释方法**
## @BeforeEach 注释用法
## `@BeforeEach`注释用法
使用`@BeforeEach`注释方法,如下所示:
......@@ -16,7 +16,7 @@ public void initEach(){
```
`@BeforeEach` **带注释的方法不得为静态方法**,否则它将引发运行时错误。
`@BeforeEach`**注释的方法不得为静态方法**,否则它将引发运行时错误。
```java
org.junit.platform.commons.JUnitException: @BeforeEach method 'public static void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.initEach()' must not be static.
......@@ -28,7 +28,7 @@ org.junit.platform.commons.JUnitException: @BeforeEach method 'public static voi
```
## @BeforeEach 注释示例
## `@BeforeEach`注释示例
让我们举个例子。 我使用了一个`Calculator`类并添加了一个`add`方法。 我将使用`@RepeatedTest`注解对其进行 5 次测试。 此注释将导致`add`测试运行 5 次。 对于每次运行的测试方法,`@BeforeEach`带注释的方法也应每次运行。
......@@ -69,7 +69,7 @@ public class BeforeEachTest {
```
其中 Calculator 类是:
其中`Calculator`类是:
```java
package com.howtodoinjava.junit5.examples;
......@@ -106,7 +106,7 @@ Running test -> 5
```
显然,每次测试方法调用都将`@BeforeEach`注释的`initEach()`方法称为**。**
显然,每次测试方法调用时都调用`@BeforeEach`注释的`initEach()`方法。
学习愉快!
......
# JUnit 5 @AfterEach 注释示例
# JUnit 5 `@AfterEach`注释示例
> 原文: [https://howtodoinjava.com/junit5/after-each-annotation-example/](https://howtodoinjava.com/junit5/after-each-annotation-example/)
JUnit 5 [@AfterEach](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/AfterEach.html) 注释替换了 JUnit 4 中的`@After`注释。它用于表示应在当前类中的每个`@Test`方法之后执行**注释方法。 。**
JUnit 5 [`@AfterEach`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/AfterEach.html) 注释替换了 JUnit 4 中的`@After`注释。它用于表示应在当前类中的每个`@Test`方法之后执行**注释方法**
## @AfterEach 注释用法
## `@AfterEach`注释用法
使用`@AfterEach`注释方法,如下所示:
......@@ -28,7 +28,7 @@ org.junit.platform.commons.JUnitException: @AfterEach method 'public static void
```
## @AfterEach 注释示例
## `@AfterEach`注释示例
让我们举个例子。 我使用了一个`Calculator`类并添加了一个`add`方法。 我将使用`@RepeatedTest`注解对其进行 5 次测试。 此注释将导致`add`测试运行 5 次。 对于每次运行的测试方法,`@AfterEach`带注释的方法也应每次运行。
......@@ -70,7 +70,7 @@ public class AfterAnnotationsTest {
```
其中 Calculator 类是:
其中`Calculator`类是:
```java
package com.howtodoinjava.junit5.examples;
......@@ -106,7 +106,7 @@ After All cleanUp() method called
```
显然,每次测试方法调用都将`@AfterEach`注释的`cleanUpEach()`方法称为**。**
显然,每次测试方法调用时都将调用`@AfterEach`注释的`cleanUpEach()`方法。
学习愉快!
......
# JUnit 5 @AfterAll 注释示例
# JUnit 5 `@AfterAll`注释示例
> 原文: [https://howtodoinjava.com/junit5/after-all-annotation-example/](https://howtodoinjava.com/junit5/after-all-annotation-example/)
JUnit 5 [@AfterAll](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/AfterAll.html) 注释替换了 JUnit 4 中的`@AfterClass`注释。它用于表示应在当前测试类中的所有测试之后执行**注释方法。**
JUnit 5 [`@AfterAll`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/AfterAll.html) 注释替换了 JUnit 4 中的`@AfterClass`注释。它用于表示应在当前测试类中的所有测试之后执行**注释方法**
## @AfterAll 注释用法
## `@AfterAll`注释用法
使用`@AfterAll`注释方法,如下所示:
......@@ -70,7 +70,7 @@ public class AfterAnnotationsTest {
```
其中 Calculator 类是:
其中`Calculator`类是:
```java
package com.howtodoinjava.junit5.examples;
......
# JUnit 5 @RepeatedTest 注释示例
# JUnit 5 `@RepeatedTest`注释示例
> 原文: [https://howtodoinjava.com/junit5/repeated-test-annotation-example/](https://howtodoinjava.com/junit5/repeated-test-annotation-example/)
通过 JUnit 5 [@RepeatedTest](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/RepeatedTest.html) 注释,可以编写可以多次运行的**可重复测试模板**。 频率可以配置为`@RepeatedTest`注释的参数。
通过 JUnit 5 [`@RepeatedTest`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/RepeatedTest.html)注释,可以编写可以多次运行的**可重复测试模板**。 频率可以配置为`@RepeatedTest`注释的参数。
## 1\. @RepeatedTest 注释用法
## 1\. `@RepeatedTest`注释用法
要创建可重复的测试模板方法,请使用`@RepeatedTest`注释该方法。
......@@ -20,7 +20,7 @@ void addNumber(TestInfo testInfo) {
在上面的代码中,`addNumber()`测试将重复 5 次。
请注意,每次重复测试**的行为都类似于常规`@Test`方法的执行,并且完全支持相同的生命周期回调和扩展**。 这意味着对于每个单独的调用,将在适合它们的测试生命周期的地方调用[`@BeforeEach`](//howtodoinjava.com/junit-5/before-each-annotation-example/)`@AfterEach`带注释的方法。
请注意,每次重复测试的行为**都类似于常规`@Test`方法的执行,并且完全支持相同的生命周期回调和扩展**。 这意味着对于每个单独的调用,将在适合它们的测试生命周期的地方调用[`@BeforeEach`](//howtodoinjava.com/junit-5/before-each-annotation-example/)`@AfterEach`带注释的方法。
```java
package com.howtodoinjava.junit5.examples;
......@@ -129,14 +129,14 @@ public class JUnit5AnnotationsExample
![JUnit 5 Repeated Test Display Names](img/b46da3713bac1ef2f67c614c4f1a4bd0.png)
JUnit 5 Repeated Test Display Names
JUnit 5 重复测试的显示名称
您可以使用两种预定义格式之一,即`RepeatedTest.LONG_DISPLAY_NAME``RepeatedTest.SHORT_DISPLAY_NAME`。 如果未指定,则`SHORT_DISPLAY_NAME`是默认格式。
* `RepeatedTest.LONG_DISPLAY_NAME`{displayName} :: {totalRepetitions}的重复{currentRepetition}
* `RepeatedTest.SHORT_DISPLAY_NAME`{totalRepetitions}的重复{currentRepetition}
* `RepeatedTest.LONG_DISPLAY_NAME``{displayName} :: repetition {currentRepetition} of {totalRepetitions}`
* `RepeatedTest.SHORT_DISPLAY_NAME``repetition {currentRepetition} of {totalRepetitions}`
```java
@DisplayName("Add operation test")
......@@ -147,9 +147,9 @@ void addNumber(TestInfo testInfo) {
```
## 3\. RepetitionInfo 接口
## 3\. `RepetitionInfo`接口
[RepetitionInfo](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/RepetitionInfo.html) 用于将有关重复测试的当前重复的信息注入`@RepeatedTest``@BeforeEach``@AfterEach`方法中。
[`RepetitionInfo`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/RepetitionInfo.html)用于将有关重复测试的当前重复的信息注入`@RepeatedTest``@BeforeEach``@AfterEach`方法中。
```java
@RunWith(JUnitPlatform.class)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册