此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
Spring JUnit 4 测试注释
@IfProfileValue
@IfProfileValue
表示为特定测试启用了带注释的测试
环境。如果配置的ProfileValueSource
返回匹配的value
对于
提供name
,则测试已启用。否则,测试将被禁用,并且实际上
忽视。
您可以申请@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
其中,列表为values
(使用OR
semantics) 实现对 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 | 将测试的时间段设置为 1 秒。 |
@Timed(millis = 1000) (1)
fun testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to run
}
1 | 将测试的时间段设置为 1 秒。 |
Spring的@Timed
注解的语义与 JUnit 4 的@Test(timeout=…)
支持。具体来说,由于 JUnit 4 处理测试执行超时的方式
(即,通过在单独的Thread
),@Test(timeout=…)
如果测试时间过长,则抢先使测试失败。Spring的@Timed
,另一方面
hand 不会抢先使测试失败,而是等待测试完成
在失败之前。
@Repeat
@Repeat
表示必须重复运行带注释的测试方法。的
在注释中指定要运行测试方法的时间。
要重复的执行范围包括测试方法本身的执行,如
以及测试夹具的任何设置或拆除。当与SpringMethodRule
,范围还包括
准备测试实例TestExecutionListener
实现。这
以下示例显示了如何使用@Repeat
注解:
-
Java
-
Kotlin
@Repeat(10) (1)
@Test
public void testProcessRepeatedly() {
// ...
}
1 | 重复此测试十次。 |
@Repeat(10) (1)
@Test
fun testProcessRepeatedly() {
// ...
}
1 | 重复此测试十次。 |