此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 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 重复此测试十次。