Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
howtodoinjava-zh
提交
41041887
H
howtodoinjava-zh
项目概览
OpenDocCN
/
howtodoinjava-zh
大约 1 年 前同步成功
通知
1
Star
82
Fork
8
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
howtodoinjava-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
41041887
编写于
6月 05, 2020
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
2020-06-05 11:54:33
上级
a56acb15
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
32 addition
and
32 deletion
+32
-32
docs/misc/161.md
docs/misc/161.md
+6
-6
docs/misc/162.md
docs/misc/162.md
+7
-7
docs/misc/163.md
docs/misc/163.md
+6
-6
docs/misc/164.md
docs/misc/164.md
+4
-4
docs/misc/165.md
docs/misc/165.md
+9
-9
未找到文件。
docs/misc/161.md
浏览文件 @
41041887
# 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
;
...
...
docs/misc/162.md
浏览文件 @
41041887
# 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()`
方法。
学习愉快!
...
...
docs/misc/163.md
浏览文件 @
41041887
# 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()`
方法。
学习愉快!
...
...
docs/misc/164.md
浏览文件 @
41041887
# 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
;
...
...
docs/misc/165.md
浏览文件 @
41041887
# 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录