Spring JUnit 4 测试注解
以下注解仅在与 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> 重复此测试十次。