|
对于最新稳定版本,请使用Spring Framework 7.0.1! |
Spring JUnit 4 测试注释
@IfProfileValue
@IfProfileValue表示注释测试已为特定测试启用
环境。如果配置ProfileValueSource返回匹配结果值对于
提供名称,测试已启用。否则,测试将被禁用,实际上,
忽视。
你可以申请@IfProfileValue无论是课堂层面、方法层面,还是两者兼有。
类级用法@IfProfileValue对于任何
该类或其子类内的方法。具体来说,如果测试是
在类级和方法层面都实现了。缺失@IfProfileValue表示测试是隐式启用的。这类似于JUnit 4的语义@Ignore注释,除了@Ignore总是禁用测试。
以下示例展示了一个具有@IfProfileValue注解:
-
Java
-
Kotlin
@IfProfileValue(name="java.vendor", value="Oracle Corporation") (1)
@Test
public void testProcessWhichRunsOnlyOnOracleJvm() {
// some logic that should run only on Java VMs from Oracle Corporation
}
| 1 | 只有当 Java 厂商是“Oracle Corporation”时才进行此测试。 |
@IfProfileValue(name="java.vendor", value="Oracle Corporation") (1)
@Test
fun testProcessWhichRunsOnlyOnOracleJvm() {
// some logic that should run only on Java VMs from Oracle Corporation
}
| 1 | 只有当 Java 厂商是“Oracle Corporation”时才进行此测试。 |
或者,你也可以配置@IfProfileValue附带值(其中或语义学)以实现 JUnit 4 环境中测试组类似 TestNG 的支持。
请考虑以下例子:
-
Java
-
Kotlin
@IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"}) (1)
@Test
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
// some logic that should run only for unit and integration test groups
}
| 1 | 运行此测试以进行单元测试和集成测试。 |
@IfProfileValue(name="test-groups", values=["unit-tests", "integration-tests"]) (1)
@Test
fun testProcessWhichRunsForUnitOrIntegrationTestGroups() {
// some logic that should run only for unit and integration test groups
}
| 1 | 运行此测试以进行单元测试和集成测试。 |
@ProfileValueSourceConfiguration
@ProfileValueSourceConfiguration是指定类型类型的类级注释
之ProfileValueSource用于检索通过@IfProfileValue注解。如果@ProfileValueSourceConfiguration对于 不被宣告
测试SystemProfileValueSource默认使用。以下示例展示了如何
用@ProfileValueSourceConfiguration:
-
Java
-
Kotlin
@ProfileValueSourceConfiguration(CustomProfileValueSource.class) (1)
public class CustomProfileValueSourceTests {
// class body...
}
| 1 | 使用自定义的配置文件值源。 |
@ProfileValueSourceConfiguration(CustomProfileValueSource::class) (1)
class CustomProfileValueSourceTests {
// class body...
}
| 1 | 使用自定义的配置文件值源。 |
@Timed
@Timed表示注释测试方法必须在指定时间内完成执行
时间段(以毫秒计)。如果文本执行时间超过指定时间
结果测试失败了。
时间段包括测试方法本身的运行、测试的任何重复(参见@Repeat),以及测试灯具的任何安装或拆卸。如下
示例展示了如何使用它:
-
Java
-
Kotlin
@Timed(millis = 1000) (1)
public void testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to run
}
| 1 | 测试时间设置为一秒。 |
@Timed(millis = 1000) (1)
fun testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to run
}
| 1 | 测试时间设置为一秒。 |
斯普林斯@Timed注释的语义与 JUnit 4 不同@Test(timeout=...)支持。具体来说,是因为 JUnit 4 处理测试执行超时的方式
(即通过在另一个程序中执行测试方法线),@Test(timeout=...)如果测试时间过长,则预先不及格。斯普林斯@Timed,另一方面
Hand,不会预先失败,而是等待测试完成
然后就失败了。
@Repeat
@Repeat表示注释测试方法必须反复运行。的数量
注释中规定测试方法运行的时间。
需要重复执行的范围包括测试方法本身的执行,具体为
还有测试灯具的安装或拆卸。当与春季法则,该范围还包括
测试实例的准备方法:TestExecutionListener实现。这
以下示例展示了如何使用@Repeat注解:
-
Java
-
Kotlin
@Repeat(10) (1)
@Test
public void testProcessRepeatedly() {
// ...
}
| 1 | 重复这个测试十次。 |
@Repeat(10) (1)
@Test
fun testProcessRepeatedly() {
// ...
}
| 1 | 重复这个测试十次。 |