此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
Spring JUnit 4 测试注释
自 Spring Framework 7.0 以来,JUnit 4 支持已被弃用,取而代之的是 |
仅当与 SpringRunner、Spring 的 JUnit 4 规则或 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
语义)来实现对 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 | 将测试的时间段设置为一秒。 |
Spring的@Timed
注释的语义与 JUnit 4 的语义不同@Test(timeout=…)
支持。具体来说,由于 JUnit 4 处理测试执行超时的方式
(即,通过在单独的Thread
),@Test(timeout=…)
如果测试时间过长,则先发制人地使测试失败。Spring的@Timed
,另一方面
手,不会先发制人地通过测试,而是等待测试完成
在失败之前。
@Repeat
@Repeat
表示必须重复运行带注释的测试方法。的数量
测试方法的运行次数在注释中指定。
要重复的执行范围包括测试方法本身的执行,如
以及测试夹具的任何安装或拆卸。当与SpringMethodRule
,
该范围还包括通过以下方式准备测试实例TestExecutionListener
实现。以下示例演示如何使用@Repeat
注解:
-
Java
-
Kotlin
@Repeat(10) (1)
@Test
public void testProcessRepeatedly() {
// ...
}
1 | 重复此测试十次。 |
@Repeat(10) (1)
@Test
fun testProcessRepeatedly() {
// ...
}
1 | 重复此测试十次。 |