Spring JUnit Jupiter 测试注解

当与 SpringExtension 和 JUnit Jupiter(即 JUnit 中的编程模型)结合使用时,支持以下注解。

@SpringJUnitConfig

@SpringJUnitConfig 是一个组合注解,它将 JUnit Jupiter 的 @ExtendWith(SpringExtension.class) 与 Spring TestContext 框架的 @ContextConfiguration 结合在一起。它可以在类级别用作 @ContextConfiguration 的替代品。在配置选项方面,@ContextConfiguration@SpringJUnitConfig 之间的唯一区别是,组件类可以使用 @SpringJUnitConfig 中的 value 属性进行声明。

以下示例展示了如何使用 @SpringJUnitConfig 注解来指定配置类

  • Java

  • Kotlin

@SpringJUnitConfig(TestConfig.class) (1)
class ConfigurationClassJUnitJupiterSpringTests {
	// class body...
}
1 指定配置类。
@SpringJUnitConfig(TestConfig::class) (1)
class ConfigurationClassJUnitJupiterSpringTests {
	// class body...
}
1 指定配置类。

以下示例展示了如何使用 @SpringJUnitConfig 注解来指定配置文件的位置

  • Java

  • Kotlin

@SpringJUnitConfig(locations = "/test-config.xml") (1)
class XmlJUnitJupiterSpringTests {
	// class body...
}
1 指定配置文件的位置。
@SpringJUnitConfig(locations = ["/test-config.xml"]) (1)
class XmlJUnitJupiterSpringTests {
	// class body...
}
1 指定配置文件的位置。

有关更多详细信息,请参阅 上下文管理 以及 @SpringJUnitConfig@ContextConfiguration 的 javadoc。

@SpringJUnitWebConfig

@SpringJUnitWebConfig 是一个组合注解,它将 JUnit Jupiter 的 @ExtendWith(SpringExtension.class) 与 Spring TestContext 框架的 @ContextConfiguration@WebAppConfiguration 结合在一起。您可以在类级别将它用作 @ContextConfiguration@WebAppConfiguration 的替代品。在配置选项方面,@ContextConfiguration@SpringJUnitWebConfig 之间的唯一区别是,您可以使用 @SpringJUnitWebConfig 中的 value 属性声明组件类。此外,您只能通过使用 @SpringJUnitWebConfig 中的 resourcePath 属性来覆盖 @WebAppConfiguration 中的 value 属性。

以下示例展示了如何使用 @SpringJUnitWebConfig 注解来指定配置类

  • Java

  • Kotlin

@SpringJUnitWebConfig(TestConfig.class) (1)
class ConfigurationClassJUnitJupiterSpringWebTests {
	// class body...
}
1 指定配置类。
@SpringJUnitWebConfig(TestConfig::class) (1)
class ConfigurationClassJUnitJupiterSpringWebTests {
	// class body...
}
1 指定配置类。

以下示例展示了如何使用 @SpringJUnitWebConfig 注解来指定配置文件的位置

  • Java

  • Kotlin

@SpringJUnitWebConfig(locations = "/test-config.xml") (1)
class XmlJUnitJupiterSpringWebTests {
	// class body...
}
1 指定配置文件的位置。
@SpringJUnitWebConfig(locations = ["/test-config.xml"]) (1)
class XmlJUnitJupiterSpringWebTests {
	// class body...
}
1 指定配置文件的位置。

有关更多详细信息,请参阅 上下文管理 以及 @SpringJUnitWebConfig@ContextConfiguration@WebAppConfiguration 的 javadoc。

@TestConstructor

@TestConstructor 是一个可以应用于测试类的注解,用于配置测试类构造函数的参数如何从测试的 ApplicationContext 中的组件自动装配。

如果测试类上不存在或元存在 @TestConstructor,将使用默认的 *测试构造函数自动装配模式*。有关如何更改默认模式的详细信息,请参阅下面的提示。但请注意,构造函数上本地声明的 @Autowired@jakarta.inject.Inject 优先于 @TestConstructor 和默认模式。

更改默认测试构造函数自动装配模式

可以通过将 spring.test.constructor.autowire.mode JVM 系统属性设置为 all 来更改默认的 *测试构造函数自动装配模式*。或者,可以通过 SpringProperties 机制设置默认模式。

默认模式也可以配置为 JUnit 平台配置参数

如果未设置 spring.test.constructor.autowire.mode 属性,则测试类构造函数将不会自动装配。

@TestConstructor 仅支持与 SpringExtension 结合使用,用于 JUnit Jupiter。请注意,SpringExtension 通常会自动为您注册——例如,当使用 @SpringJUnitConfig@SpringJUnitWebConfig 等注解或 Spring Boot Test 中的各种与测试相关的注解时。

@NestedTestConfiguration

@NestedTestConfiguration 是一个可以应用于测试类的注解,用于配置 Spring 测试配置注解如何在内部测试类的封闭类层次结构中进行处理。

如果测试类、其超类型层次结构或其封闭类层次结构中不存在或元存在 @NestedTestConfiguration,将使用默认的 *封闭配置继承模式*。有关如何更改默认模式的详细信息,请参阅下面的提示。

更改默认封闭配置继承模式

默认的 *封闭配置继承模式* 是 INHERIT,但可以通过将 spring.test.enclosing.configuration JVM 系统属性设置为 OVERRIDE 来更改。或者,可以通过 SpringProperties 机制设置默认模式。

Spring TestContext 框架 尊重以下注解的 @NestedTestConfiguration 语义。

@NestedTestConfiguration 的使用通常只在与 JUnit Jupiter 中的 @Nested 测试类结合时才有意义;但是,可能还有其他支持 Spring 和嵌套测试类的测试框架使用此注解。

有关示例和更多详细信息,请参阅 @Nested 测试类配置

@EnabledIf

@EnabledIf 用于表示如果提供的 expression 计算结果为 true,则应运行带注解的 JUnit Jupiter 测试类或测试方法。具体来说,如果表达式计算结果为 Boolean.TRUE 或等于 trueString(忽略大小写),则启用测试。当应用于类级别时,该类中的所有测试方法默认也会自动启用。

表达式可以是以下任何一种

  • Spring Expression Language (SpEL) 表达式。例如: @EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")

  • Spring Environment 中可用的属性的占位符。例如: @EnabledIf("${smoke.tests.enabled}")

  • 文本字面量。例如: @EnabledIf("true")

然而,请注意,不是属性占位符动态解析结果的文本字面量没有实际价值,因为 @EnabledIf("false") 等同于 @Disabled,而 @EnabledIf("true") 在逻辑上是无意义的。

您可以将 @EnabledIf 用作元注解来创建自定义组合注解。例如,您可以创建自定义的 @EnabledOnMac 注解,如下所示

  • Java

  • Kotlin

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@EnabledIf(
	expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
	reason = "Enabled on Mac OS"
)
public @interface EnabledOnMac {}
@Target(AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@EnabledIf(
		expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
		reason = "Enabled on Mac OS"
)
annotation class EnabledOnMac {}

@EnabledOnMac 仅作为可能性示例。如果您有完全相同的用例,请使用 JUnit Jupiter 中内置的 @EnabledOnOs(MAC) 支持。

自 JUnit 5.7 起,JUnit Jupiter 也包含一个名为 @EnabledIf 的条件注解。因此,如果您希望使用 Spring 的 @EnabledIf 支持,请确保从正确的包中导入注解类型。

@DisabledIf

@DisabledIf 用于表示如果提供的 expression 计算结果为 true,则应禁用带注解的 JUnit Jupiter 测试类或测试方法,并且不应运行。具体来说,如果表达式计算结果为 Boolean.TRUE 或等于 trueString(忽略大小写),则禁用测试。当应用于类级别时,该类中的所有测试方法也会自动禁用。

表达式可以是以下任何一种

  • Spring Expression Language (SpEL) 表达式。例如: @DisabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")

  • Spring Environment 中可用的属性的占位符。例如: @DisabledIf("${smoke.tests.disabled}")

  • 文本字面量。例如: @DisabledIf("true")

然而,请注意,不是属性占位符动态解析结果的文本字面量没有实际价值,因为 @DisabledIf("true") 等同于 @Disabled,而 @DisabledIf("false") 在逻辑上是无意义的。

您可以将 @DisabledIf 用作元注解来创建自定义组合注解。例如,您可以创建自定义的 @DisabledOnMac 注解,如下所示

  • Java

  • Kotlin

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@DisabledIf(
	expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
	reason = "Disabled on Mac OS"
)
public @interface DisabledOnMac {}
@Target(AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@DisabledIf(
		expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
		reason = "Disabled on Mac OS"
)
annotation class DisabledOnMac {}

@DisabledOnMac 仅作为可能性示例。如果您有完全相同的用例,请使用 JUnit Jupiter 中内置的 @DisabledOnOs(MAC) 支持。

自 JUnit 5.7 起,JUnit Jupiter 也包含一个名为 @DisabledIf 的条件注解。因此,如果您希望使用 Spring 的 @DisabledIf 支持,请确保从正确的包中导入注解类型。

© . This site is unofficial and not affiliated with VMware.