Disable servlet filters for unit tests

Viewed 2064

In Spring Boot application I am unit testing controllers using MockMvc. I have a servlet filter that needs to run for every request but I want to ignore it when running unit tests and making requests through MockMvc.

The reason is that my filter is a @Component and it has got another bean @Autowired (call it theBean) and when the unit tests run some of them fail because if theBean is not in the context and some of them pass because the controllers also use theBean

What I am looking to do is to disable the servlet filter when it's running the MVC tests using MockMvc, any ideas?

2 Answers

you can use condition bean expression for your filter class

`@ConditionalOnProperty(name = "spring.profile.test", matchIfMissing = true)`

set spring.profile.test as true in property file

Related