Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable jersey filters for unit tests only (using spring-boot)

I have a spring-boot application with Jersey as the REST engine. I am using Jersey filters for authenticating each request. The question is what is the appropriate way to disable these filters or provide mock filters during unit tests of the REST controllers (with JUnit)?

Here is what I have:

  • Spring boot application:

    @PropertySource("classpath:security-api.properties")
    @SpringBootApplication
    public class MyApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    
  • Jersey Configuration file:

    @Configuration
    @ApplicationPath("/some/path")
    public class MyJerseyConfiguration extends ResourceConfig {
    
        public AnalyzerJerseyConfiguration() {
            packages(BaseController.class.getPackage().getName());
            register(AuthorizationRequestFilter.class);
            register(AuthorizationResponseFilter.class);
        }
    }
    
like image 302
Guy Hudara Avatar asked Jan 26 '26 08:01

Guy Hudara


1 Answers

Probably your best bet is to use Spring Profiles. You can mark the ResourceConfig in your post as, as a "production" configuration, while for tests, create a new ResourceConfig with a "test" profile. Then in your tests just set the active profile to "test", and in production, set the active profile to "production"

@Profile("production")
@Configuration
@ApplicationPath("/some/path")
public class MyJerseyConfiguration extends ResourceConfig {

    public AnalyzerJerseyConfiguration() {
        packages(BaseController.class.getPackage().getName());
        register(AuthorizationRequestFilter.class);
        register(AuthorizationResponseFilter.class);
    }
}

@Profile("test")
@Configuration
@ApplicationPath("/some/path")
public class TestConfiguration extends ResourceConfig {

    public AnalyzerJerseyConfiguration() {
        packages(BaseController.class.getPackage().getName());
        register(TestRequestFilter.class);
        register(TestResponseFilter.class);
    }
}

@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
public class TestClass {}

You can also set the active profiles in your property configuration files (as mentioned in the above link).

If you want different configurations for different tests, instead of creating a bunch of ResourceConfig subclasses, it might just make more sense to create different spring configurations. You can just create @Bean methods for different configurations.

@Profile("test")
@Configuration
public class TestOneConfig {
    @Bean
    public ResourceConfig resourceConfig() {
        return new ResourceConfig()
            .register(Something.class);
    }
}

@Profile("test")
@Configuration
public class TestTwoConfig {
    @Bean
    public ResourceConfig resourceConfig() {
        return new ResourceConfig()
            .register(DifferentSomething.class);
    }
}

Whichever one you want to use, just add that to the test configuration.

@ActiveProfiles("test")
@SpringApplicationConfiguration(classs={MainApp.class, TestOneConfig.class})
public class TheTest {}

With Spring Boot 1.4 (Not yet released), they introduce a ResourceConfigCustomizer. This might make it a little cleaner, without the need to create a "test" ResourceConfig, you might be able to do something like

@Component
public class TestCustomizer implements ResourceConfigCustomizer {
    @Override
    public void customize(ResourceConfig config) {
        config.getClasses().remove(AuthorizationRequestFilter.class);
        config.register(TestRequestFilter.class);
    }
}

and register this in a test configuration.

like image 198
Paul Samsotha Avatar answered Jan 28 '26 08:01

Paul Samsotha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!