我正在構建一個應用程序spring-boot
。爲了避免粘會話相關的問題,我建立了一個Redis的會話存儲在pom.xml
添加這些行:防止在單元測試中連接到Redis服務器
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
和application.properties這些線路:
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379
它的工作就像魅力。即使我沒有使用註釋@EnableRedisHttpSession
,我也很驚訝。一開始,我發現它很好。
問題:我有一個實際應用程序的Spring配置,還有一個專用於單元測試的Spring配置。如果我沒有在測試環境中安裝Redis服務器,則Redis連接在單元測試環境中無用,並使測試失敗。
我最終可以將Mock Redis安裝爲Maven依賴項,但如果我找到禁用此無用連接的方法,它會更乾淨。
有什麼想法?
春天啓動的'SessionAutoConfiguration'踢你可以在你的應用程序中排除。 ('@SpringBootApplication'或properties),參見[here](https://github.com/pivotalsoftware/pivotal-cla/blob/b35645d709fc4bae31b353eaddbd15374cf2570f/src/main/java/io/pivotal/cla/GithubClaApplication.java) – mp911de
@ mp911de只有當我將它應用於應用程序conf和測試conf時,纔會添加此項。如果我只在應用程序conf上手動添加'@ EnableRedisHttpSession',它也會被測試配置檢測到。你有關於如何防止測試conf檢測應用程序conf上的'@ EnableRedisHttpSession'的想法嗎? –
「@Enable ...」註釋沒有恢復。啓用已啓用。我認爲個人資料可以幫助你。 – mp911de