对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-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检索通过@IfProfileValue注解。 如果@ProfileValueSourceConfiguration未声明为 测试SystemProfileValueSource默认使用。以下示例演示如何 用@ProfileValueSourceConfiguration:spring-doc.cadn.net.cn

@ProfileValueSourceConfiguration(CustomProfileValueSource.class) (1)
public class CustomProfileValueSourceTests {
	// class body...
}
1 使用自定义配置文件值源。
@ProfileValueSourceConfiguration(CustomProfileValueSource::class) (1)
class CustomProfileValueSourceTests {
	// class body...
}
1 使用自定义配置文件值源。

@Timed

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

该时间段包括运行测试方法本身、测试的任何重复(请参阅@Repeat),以及测试夹具的任何设置或拆卸。以下示例显示如何使用它: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 重复此测试十次。