Spring 5 Testing with @EnabledIf Annotation – 使用@EnabledIf注解的Spring 5测试

最后修改: 2017年 11月 23日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this quick article, we’ll discover the @EnabledIf and @DisabledIf annotations in Spring 5 using JUnit 5.

在这篇快速文章中,我们将使用JUnit 5发现Spring 5中的@EnabledIf@DisabledIf注释。

Simply put, those annotations make it possible to disable/enable particular test if a specified condition is met.

简单地说,这些注释使我们有可能在满足指定条件的情况下禁用/启用特定的测试。

We’ll use a simple test class to show how these annotations work:

我们将使用一个简单的测试类来展示这些注解如何工作。

@SpringJUnitConfig(Spring5EnabledAnnotationIntegrationTest.Config.class)
public class Spring5EnabledAnnotationIntegrationTest {
 
    @Configuration
    static class Config {}
}

2. @EnabledIf

2.@EnabledIf

Let’s add to our class this simple test with a text literal “true”:

让我们把这个简单的测试添加到我们的类中,其中有一个文本字面“true”

@EnabledIf("true")
@Test
void givenEnabledIfLiteral_WhenTrue_ThenTestExecuted() {
    assertTrue(true);
}

If we run this test, it executes normally.

如果我们运行这个测试,它可以正常执行。

However, if we replace the provided String with “false” it’s not executed:

然而,如果我们将提供的String替换为“false”,则不会被执行:

Keep in mind that if you want to statically disable a test, there’s a dedicated @Disabled annotation for this.

请记住,如果你想静态地禁用一个测试,有一个专门的@Disabled注解用于此。

3. @EnabledIf With a Property Placeholder

3.@EnabledIf 带有属性占位符的

A more practical way of using @EnabledIf is by using a property placeholder:

使用@EnabledIf的一个更实用的方法是通过使用一个属性占位符。

@Test
@EnabledIf(
  expression = "${tests.enabled}", 
  loadContext = true)
void givenEnabledIfExpression_WhenTrue_ThenTestExecuted() {
    // ...
}

First of all, we need to make sure that the loadContext parameter is set to true so that the Spring context gets loaded.

首先,我们需要确保loadContext参数被设置为true,以便Spring上下文被加载。

By default, this parameter is set to false to avoid unnecessary context loading.

默认情况下,该参数被设置为false以避免不必要的上下文加载。

4. @EnabledIf With a SpEL Expression

4.@EnabledIf与SpEL表达式

Finally, we can use the annotation with Spring Expression Language (SpEL) expressions.

最后,我们可以用Spring表达式语言(SpEL)表达式来使用该注释。

For example, we can enable tests only when running JDK 1.8

例如,我们可以只在运行JDK 1.8时启用测试

@Test
@EnabledIf("#{systemProperties['java.version'].startsWith('1.8')}")
void givenEnabledIfSpel_WhenTrue_ThenTestExecuted() {
    assertTrue(true);
}

5. @DisabledIf

5.@DisabledIf

This annotation is the opposite of @EnabledIf.

该注释与@EnabledIf.相反。

For example, we can disable test when running on Java 1.7:

例如,我们可以在Java 1.7上运行时禁用测试。

@Test
@DisabledIf("#{systemProperties['java.version'].startsWith('1.7')}")
void givenDisabledIf_WhenTrue_ThenTestNotExecuted() {
    assertTrue(true);
}

6. Conclusion

6.结论

In this brief article, we went through several examples of the usage of @EnabledIf and @DisabledIf annotations in JUnit 5 tests using the SpringExtension.

在这篇简短的文章中,我们通过几个例子来说明在JUnit 5测试中使用@EnabledIf@DisabledIf注解的用法,使用SpringExtension

The full source code for the examples is available over on GitHub.

例子的完整源代码可在GitHub上获得over