Spring JUnit 4 测试注解
自 Spring Framework 7.0 起,JUnit 4 支持已被弃用,取而代之的是
SpringExtension
和 JUnit Jupiter。
以下注解仅在与 SpringRunner、 Spring 的 JUnit 4 规则 或 Spring 的 JUnit 4 支持类 结合使用时才受支持:
@IfProfileValue
@IfProfileValue 指示带注解的测试类或测试方法针对特定的测试环境启用。如果配置的
ProfileValueSource 为提供的 name 返回匹配的 value,则测试启用。否则,测试禁用,并实际上被忽略。
你可以在类级别、方法级别或两者都应用 @IfProfileValue。类级别使用 @IfProfileValue
对于该类或其子类中的任何方法都优先于方法级别使用。具体来说,如果测试在类级别和方法级别都启用,则它才启用。
缺少 @IfProfileValue 意味着测试隐式启用。这类似于 JUnit 4 的 @Ignore
注解的语义,只不过 @Ignore 的存在总是禁用测试。
以下示例显示了一个带有 @IfProfileValue 注解的测试:
- Java
-
@IfProfileValue(name="java.vendor", value="Oracle Corporation") [id="CO1-1"][id="CO1-1"][id="CO1-1"](1) @Test public void testProcessWhichRunsOnlyOnOracleJvm() { // some logic that should run only on Java VMs from Oracle Corporation }
<1> 仅当 Java 供应商为 "Oracle Corporation" 时才运行此测试。
- Kotlin
-
@IfProfileValue(name="java.vendor", value="Oracle Corporation") [id="CO2-1"][id="CO1-2"][id="CO2-1"](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
-
@IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"}) [id="CO3-1"][id="CO1-3"][id="CO3-1"](1) @Test public void testProcessWhichRunsForUnitOrIntegrationTestGroups() { // some logic that should run only for unit and integration test groups }
<1> 针对单元测试和集成测试运行此测试。
- Kotlin
-
@IfProfileValue(name="test-groups", values=["unit-tests", "integration-tests"]) [id="CO4-1"][id="CO1-4"][id="CO4-1"](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
-
@ProfileValueSourceConfiguration(CustomProfileValueSource.class) [id="CO5-1"][id="CO1-5"][id="CO5-1"](1) public class CustomProfileValueSourceTests { // class body... }
<1> 使用自定义配置文件值源。
- Kotlin
-
@ProfileValueSourceConfiguration(CustomProfileValueSource::class) [id="CO6-1"][id="CO1-6"][id="CO6-1"](1) class CustomProfileValueSourceTests { // class body... }
<1> 使用自定义配置文件值源。
@Timed
@Timed 指示带注解的测试方法必须在指定时间段(以毫秒为单位)内完成执行。如果测试执行时间超过指定时间段,则测试失败。
时间段包括运行测试方法本身、测试的任何重复(参见 @Repeat),以及测试夹具的任何设置或拆卸。
以下示例展示了如何使用它:
- Java
-
@Timed(millis = 1000) [id="CO7-1"][id="CO1-7"][id="CO7-1"](1) public void testProcessWithOneSecondTimeout() { // some logic that should not take longer than 1 second to run }
<1> 将测试的时间段设置为一秒。
- Kotlin
-
@Timed(millis = 1000) [id="CO8-1"][id="CO1-8"][id="CO8-1"](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
-
@Repeat(10) [id="CO9-1"][id="CO1-9"][id="CO9-1"](1) @Test public void testProcessRepeatedly() { // ... }
<1> 将此测试重复十次。
- Kotlin
-
@Repeat(10) [id="CO10-1"][id="CO1-10"][id="CO10-1"](1) @Test fun testProcessRepeatedly() { // ... }
<1> 将此测试重复十次。