Spring JUnit 4 测试注解

自 Spring Framework 7.0 起,JUnit 4 支持已被弃用,取而代之的是 SpringExtension 和 JUnit Jupiter。

以下注解仅在使用 SpringRunnerSpring 的 JUnit 4 规则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 语义),以在 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 注解检索配置文件值时使用哪种类型的 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 重复此测试十次。
© . This site is unofficial and not affiliated with VMware.