|
对于最新的稳定版本,请使用 Spring Framework 7.0.6! |
Spring JUnit 4 测试注解
@IfProfileValue
@IfProfileValue 表示带注解的测试在特定测试环境下被启用。如果配置的 ProfileValueSource 为提供的 name 返回匹配的 value,则测试被启用。否则测试将被禁用且实际上被忽略。
您可以在类级别、方法级别或两者同时应用@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语义)来实现类似TestNG的测试组支持,该功能适用于JUnit 4环境。
请看以下示例:
-
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 是一个类级别注解,用于指定在检索通过 @IfProfileValue 注解配置的配置文件值时使用何种类型的 ProfileValueSource。如果测试中未声明 @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 | 重复此测试十次。 |