对于最新稳定版本,请使用 Spring Framework 7.0.6spring-doc.cadn.net.cn

Spring JUnit 4 测试注解

@IfProfileValue

@IfProfileValue 表示带注解的测试在特定测试环境中启用。如果配置的 ProfileValueSource 为提供的 value 返回匹配的 name,则该测试将被启用;否则,该测试将被禁用,并实际上被忽略。spring-doc.cadn.net.cn

你可以在类级别、方法级别,或同时在这两个级别上应用 @IfProfileValue。 在类或其子类中的任何方法上,类级别的 @IfProfileValue 用法优先于方法级别的用法。 具体来说,只有当一个测试在类级别和方法级别都被启用时,该测试才会被启用。 如果没有使用 @IfProfileValue,则表示该测试默认是启用的。 这与 JUnit 4 中 @Ignore 注解的语义类似,不同之处在于 @Ignore 的存在总是会禁用一个测试。spring-doc.cadn.net.cn

以下示例展示了一个带有 @IfProfileValue 注解的测试:spring-doc.cadn.net.cn

@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 的测试组支持。 请看以下示例:spring-doc.cadn.net.cn

@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 注解获取配置的 profile 值时应使用哪种类型的 @IfProfileValue。如果测试类未声明 @ProfileValueSourceConfiguration,则默认使用 SystemProfileValueSource。以下示例展示了如何使用 @ProfileValueSourceConfigurationspring-doc.cadn.net.cn

@ProfileValueSourceConfiguration(CustomProfileValueSource.class) (1)
public class CustomProfileValueSourceTests {
	// class body...
}
1 使用自定义的 profile 值来源。
@ProfileValueSourceConfiguration(CustomProfileValueSource::class) (1)
class CustomProfileValueSourceTests {
	// class body...
}
1 使用自定义的 profile 值来源。

@Timed

@Timed 表示被注解的测试方法必须在指定的时间段(以毫秒为单位)内完成执行。如果测试执行时间超过指定的时间段,则测试失败。spring-doc.cadn.net.cn

该时间段包括执行测试方法本身、测试的任何重复(参见 @Repeat),以及测试夹具(test fixture)的任何设置或清理工作。以下示例展示了如何使用它:spring-doc.cadn.net.cn

@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 则不会提前使测试失败,而是等待测试完成后再判定其是否失败。spring-doc.cadn.net.cn

@Repeat

@Repeat 表示被注解的测试方法必须重复运行。测试方法需要运行的次数在注解中指定。spring-doc.cadn.net.cn

重复执行的范围包括测试方法本身的执行,以及测试夹具的任何设置或清理。当与 SpringMethodRule一起使用时,该范围还额外包括通过TestExecutionListener实现来准备测试实例。以下示例展示了如何使用@Repeat注解:spring-doc.cadn.net.cn

@Repeat(10) (1)
@Test
public void testProcessRepeatedly() {
	// ...
}
1 重复此测试十次。
@Repeat(10) (1)
@Test
fun testProcessRepeatedly() {
	// ...
}
1 重复此测试十次。