Disable Redis AutoConfig in spring boot when testing

10,299

Solution 1

  • Try excluding this two auto-configuration classes in your test properties file:
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

or

  • exclude org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
    and set: spring.data.redis.repositories.enabled=false

Solution 2

With YAML syntax (& Spring Boot):

spring.autoconfigure:
  exclude:
    - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
    - org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

If you have SystemEnvironmentPropertySource in you app context you can use environment variable SPRING_AUTOCONFIGURE_EXCLUDE separating items with comma:

SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

Solution 3

If you dont want to change any files/code, you can also do this with an environment variable:

SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
Share:
10,299
Christian Bongiorno
Author by

Christian Bongiorno

Software engineering leader with a patent and over 15 years combined software development experience. I truly enjoy collaborating with others and mentoring Junior developers while working with the product team to build world class software. You can find samples of my work: https://github.com/chb0github/ And my linked in profile: http://www.linkedin.com/pub/christian-bongiorno/3/282/732 My patent: http://www.google.com/patents/US8495068

Updated on June 22, 2022

Comments

  • Christian Bongiorno
    Christian Bongiorno almost 2 years

    I am trying to disable Redis when I am testing with spring boot. I have disabled my configuration but the auto config created a default connection and fails because it can't connect to a non-existent service. For testing I am content to just use a basic in-memory cache or a no-op cache. That doesn't work either. Here is what I have tried:

    per this issue I added said configuration to my test app properties

    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
    

    But. That gets me a bit further. But ultimately I get a NoSuchBeanDefinitionException redisTemplate - this is because redisReferenceResolver is trying to look that up.

    Looking at my debugger right now, the bean it's trying to hydrate is: org.springframework.data.redis.core.convert.ReferenceResolverImpl which is coming from spring-data-redis:1.8.0.RELEASE which is coming from this dependency: compile('org.springframework.boot:spring-boot-starter-data-redis') . I admit, the bean name is a bit misleading. The type it actually resolves to is not

    The only other reference to redis is in our hibernate support.

    Can someone explain how to turn this off for testing?