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”时运行此测试。

或者,您可以使用 values 列表(具有 OR 语义)配置 @IfProfileValue,以在 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 是一个可以应用于测试类的注解,用于指定通过 @IfProfileValue 注解配置的 profile 值时要使用的 ProfileValueSource 类型。如果测试未声明 @ProfileValueSourceConfiguration,则默认使用 SystemProfileValueSource。以下示例展示了如何使用 @ProfileValueSourceConfiguration

  • Java

  • Kotlin

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

@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 将此测试重复十次。