The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5 – Spring5中的SpringJUnitConfig和SpringJUnitWebConfig注解

最后修改: 2017年 12月 1日

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

1. Introduction

1.介绍

In this quick article, we’ll take a look at the new @SpringJUnitConfig and @SpringJUnitWebConfig annotations available in Spring 5.

在这篇快速文章中,我们将看看Spring 5中新的@SpringJUnitConfig@SpringJUnitWebConfig注释。

These annotations are a composition of JUnit 5 and Spring 5 annotations that make test creation easier and faster.

这些注解是JUnit 5和Spring 5注解的组合,使测试创建更容易、更快速。

2. @SpringJUnitConfig

2、@SpringJUnitConfig

@SpringJUnitConfig combines these 2 annotations:

@SpringJUnitConfig结合了这两个注解。

  • @ExtendWith(SpringExtension.class) from JUnit 5 to run the test with the SpringExtension class and
  • @ContextConfiguration from Spring Testing to load the Spring context

Let’s create a test and use this annotation in practice:

让我们创建一个测试并在实践中使用这个注释。

@SpringJUnitConfig(SpringJUnitConfigIntegrationTest.Config.class)
public class SpringJUnitConfigIntegrationTest {

    @Configuration
    static class Config {}
}

Notice that, in contrast to the @ContextConfiguration, configuration classes are declared using the value attribute. However, resource locations should be specified with the locations attribute.

请注意,与@ContextConfiguration相反,配置类是使用value属性来声明的。然而,资源位置应该用locations属性来指定。

We can now verify that the Spring context was really loaded:

我们现在可以验证Spring上下文是否真的被加载。

@Autowired
private ApplicationContext applicationContext;

@Test
void givenAppContext_WhenInjected_ThenItShouldNotBeNull() {
    assertNotNull(applicationContext);
}

Finally, here we have the equivalent code of @SpringJUnitConfig(SpringJUnitConfigTest.Config.class):

最后,这里我们有@SpringJUnitConfig(SpringJUnitConfigTest.Config.class)的等同代码:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SpringJUnitConfigTest.Config.class)

3. @SpringJUnitWebConfig

3.@SpringJUnitWebConfig

@SpringJUnitWebConfig combines the same annotations of @SpringJUnitConfig plus the @WebAppConfiguration from Spring testing – to load the WebApplicationContext.

@SpringJUnitWebConfig 结合了@SpringJUnitConfig的相同注解以及来自Spring测试的@WebAppConfiguration–以加载WebApplicationContext

Let’s see how this annotation works:

让我们看看这个注释是如何工作的。

@SpringJUnitWebConfig(SpringJUnitWebConfigIntegrationTest.Config.class)
public class SpringJUnitWebConfigIntegrationTest {

    @Configuration
    static class Config {
    }
}

Like @SpringJUnitConfig, the configuration classes go in the value attribute and any resources are specified using the locations attribute.

@SpringJUnitConfig一样,配置类放在value属性中,任何资源都用locations属性指定。

Also, the value attribute of @WebAppConfiguration should now be specified using the resourcePath attribute. By default, this attribute is set to “src/main/webapp”.

另外,@WebAppConfigurationvalue属性现在应该使用resourcePath属性来指定。默认情况下,该属性被设置为“src/main/webapp”

Let’s now verify that the WebApplicationContext was really loaded:

现在让我们验证一下WebApplicationContext是否真的被加载。

@Autowired
private WebApplicationContext webAppContext;

@Test
void givenWebAppContext_WhenInjected_ThenItShouldNotBeNull() {
    assertNotNull(webAppContext);
}

Again, here we have the equivalent code without using @SpringJUnitWebConfig:

同样,这里我们有同等的代码,没有使用@SpringJUnitWebConfig

@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextConfiguration(classes = SpringJUnitWebConfigIntegrationTest.Config.class)

4. Conclusion

4.结论

In this brief tutorial, we showed how to use the newly introduced @SpringJUnitConfig and @SpringJUnitWebConfig annotations in Spring 5.

在这个简短的教程中,我们展示了如何使用Spring 5中新引入的@SpringJUnitConfig@SpringJUnitWebConfig注释。

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

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