Disabling OAuth2 Security for Integration Tests with @TestConfiguration

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;

public class IntegrationTestConfig {

     * Configures a security filter chain to disable OAuth2 security for integration tests.
     * @param http the {@link HttpSecurity} to configure
     * @return the {@link SecurityFilterChain} configured to disable OAuth2 security
     * @throws Exception if an error occurs while configuring security
    public SecurityFilterChain disableOAuth2Security(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(authorize -> authorize


The @TestConfiguration annotation allows for the customization of Spring Boot's application context for testing purposes, without affecting the main application configuration. In the provided code snippet, @TestConfiguration is used to define a SecurityFilterChain bean that disables OAuth2 security, simplifying integration testing by bypassing authentication and authorization steps.


  • Purpose of @TestConfiguration: This specialized configuration annotation is designed for use in tests, enabling developers to override or add additional configuration without influencing the main application context. It's particularly useful for setting up or mocking certain behaviors specific to testing scenarios.

  • Disabling OAuth2 Security: For integration tests, particularly those not focusing on security, it's often practical to bypass security constraints to directly test business logic and integration points. The provided SecurityFilterChain bean method disables CSRF protection and configures Spring Security to permit all requests, effectively neutralizing OAuth2 security for tests.

  • Integration with Test Classes: To apply this configuration, the @Import(IntegrationTestConfig.class) annotation should be added to your test classes. This ensures that the test context includes the overridden security configuration, allowing tests to run without the need for authenticating requests.


  • Simplified Testing Environment: By disabling security features that are not relevant to certain integration tests, developers can focus on the functionality and integration aspects of the application.

  • Isolated Configuration: Since @TestConfiguration is only applied to tests where it's explicitly imported, there's no risk of it affecting the production configuration or other tests.

  • Flexibility and Control: This approach provides fine-grained control over the test environment, allowing for more accurate and efficient testing of specific components or functionalities.

By strategically using @TestConfiguration to disable OAuth2 security, developers can ensure that their integration tests are both effective and efficient, focusing on the core functionality of the application under test.

Last updated

Need US based Java Developers

Visit Katyella

Katyella LLC http://katyella.com