Exclude Auto-Configuration Classes in Spring Boot Tests – 在Spring Boot测试中排除自动配置类

最后修改: 2019年 1月 5日

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

1. Overview

1.概述

In this quick tutorial, we’ll discuss how to exclude auto-configuration classes from Spring Boot tests.

在这个快速教程中,我们将讨论如何从Spring Boot测试中排除自动配置类

Spring Boot’s auto-configuration feature is very handy, as it takes care of a lot of setup for us. However, this can also be an issue during testing if we don’t want a certain auto-configuration to interfere with our tests of a module.

Spring Boot 的自动配置功能非常方便,因为它为我们解决了大量的设置问题。但是,如果我们不希望某个自动配置干扰我们对某个模块的测试,那么在测试过程中这也可能是一个问题。

A common example of this is the security auto-configuration, which we’ll also use for our examples.

一个常见的例子是安全自动配置,我们的例子中也会用到它。

2. Test Example

2.测试实例

First, we’ll take a look at our testing example.

首先,我们来看看我们的测试例子。

We’ll have a secured Spring Boot application with a simple home page.

我们将有一个安全的Spring Boot应用程序,有一个简单的主页。

When we try to access the home page without authentication, the response is “401 UNAUTHORIZED”.

当我们试图在没有认证的情况下访问主页时,响应是 “401 UNAUTHORIZED”。

Let’s see this in a test that uses REST-assured to make the call:

让我们在一个使用REST-assured来进行调用的测试中看到这一点。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class AutoConfigIntegrationTest {

    @Test
    public void givenNoAuthentication_whenAccessHome_thenUnauthorized() {
        int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
        
        assertEquals(HttpStatus.UNAUTHORIZED.value(), statusCode);
    }
    
}

On the other hand, we can access the home page successfully with authentication:

另一方面,我们可以通过认证成功访问主页。

@Test
public void givenAuthentication_whenAccessHome_thenOK() {
    int statusCode = RestAssured.given().auth().basic("john", "123")
      .get("http://localhost:8080/")
      .statusCode();
    
    assertEquals(HttpStatus.OK.value(), statusCode);
}

In the following sections, we’ll try different ways to exclude the SecurityAutoConfiguration class from our tests’ configuration.

在下面的章节中,我们将尝试不同的方法,将SecurityAutoConfiguration类从我们的测试中排除配置。

3. Using @EnableAutoConfiguration

3.使用@EnableAutoConfiguration

There are multiple ways to exclude a specific Auto-configuration class from tests’ configuration.

有多种方法可以从测试的配置中排除一个特定的自动配置类。

First, let’s see how we can use the @EnableAutoConfiguration(exclude={CLASS_NAME}) annotation:

首先,让我们看看如何使用@EnableAutoConfiguration(exclude={CLASS_NAME}) annotation

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
public class ExcludeAutoConfigIntegrationTest {

    @Test
    public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
        int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
        
        assertEquals(HttpStatus.OK.value(), statusCode);
    }
}

In this example, we excluded the SecurityAutoConfiguration class using the exclude attribute, but we can do the same with any of the auto-configuration classes.

在这个例子中,我们使用exclude属性排除了SecurityAutoConfiguration类,但我们可以对任何自动配置类进行同样的处理。

Now we can run our test that accesses the home page without authentication and it will no longer fail.

现在我们可以运行我们的测试,在没有认证的情况下访问主页,它将不再失败。

4. Using @TestPropertySource

4.使用@TestPropertySource

Next, we can use @TestPropertySource to inject the property “spring.autoconfigure.exclude:

接下来,我们可以使用@TestPropertySource来注入属性”spring.autoconfigure.exclude“/strong>。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = 
 "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration")
public class ExcludeAutoConfigIntegrationTest {
    // ...
}

Note that we need to specify the full class name (package name+simple name) for the property.

注意,我们需要为该属性指定完整的类名(包名+简名)。

5. Using Profiles

5.使用配置文件

We can also set the property “spring.autoconfigure.exclude” for our tests using profiles:

我们还可以使用配置文件为我们的测试设置属性”spring.autoconfigure.exclude“:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
public class ExcludeAutoConfigIntegrationTest {
    // ...
}

And include all “test” profile specific properties in application-test.properties:

并在application-test.properties中包括所有”test“配置文件的特定属性。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

6. Using a Custom Test Configuration

6.使用自定义测试配置

Finally, we can use a separate configuration application for our tests:

最后,我们可以为我们的测试使用一个单独的配置应用程序

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class ExcludeAutoConfigIntegrationTest {
    // ...
}

And exclude the auto-configuration class from @SpringBootApplication(exclude={CLASS_NAME}):

并从@SpringBootApplication(exclude={CLASS_NAME})/em>中排除自动配置类。

@SpringBootApplication(exclude=SecurityAutoConfiguration.class)
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

7. Conclusion

7.结论

In this article, we explored different ways to exclude auto-configuration classes from Spring Boot tests.

在这篇文章中,我们探讨了从Spring Boot测试中排除自动配置类的不同方法。

The full source code is available over on GitHub.

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