From 52ea98b4ceb631979a24aa10a2a911e921d1377e Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 7 Sep 2017 19:37:58 -0500 Subject: [PATCH] SpringWebSessionConfigurationTests close ApplicationContext --- .../SpringWebSessionConfigurationTests.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/spring-session-core/src/test/java/org/springframework/session/SpringWebSessionConfigurationTests.java b/spring-session-core/src/test/java/org/springframework/session/SpringWebSessionConfigurationTests.java index 77f4d969..976e65ca 100644 --- a/spring-session-core/src/test/java/org/springframework/session/SpringWebSessionConfigurationTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/SpringWebSessionConfigurationTests.java @@ -15,6 +15,7 @@ */ package org.springframework.session; +import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.UnsatisfiedDependencyException; @@ -36,32 +37,40 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * @author Greg Turnquist */ public class SpringWebSessionConfigurationTests { + private AnnotationConfigApplicationContext context; + + @After + public void cleanup() { + if (this.context != null) { + this.context.close(); + } + } @Test public void enableSpringWebSessionConfiguresThings() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(GoodConfig.class); - ctx.refresh(); + this.context = new AnnotationConfigApplicationContext(); + this.context.register(GoodConfig.class); + this.context.refresh(); - WebSessionManager webSessionManagerFoundByType = ctx.getBean(WebSessionManager.class); - Object webSessionManagerFoundByName = ctx.getBean(WebHttpHandlerBuilder.WEB_SESSION_MANAGER_BEAN_NAME); + WebSessionManager webSessionManagerFoundByType = this.context.getBean(WebSessionManager.class); + Object webSessionManagerFoundByName = this.context.getBean(WebHttpHandlerBuilder.WEB_SESSION_MANAGER_BEAN_NAME); assertThat(webSessionManagerFoundByType).isNotNull(); assertThat(webSessionManagerFoundByName).isNotNull(); assertThat(webSessionManagerFoundByType).isEqualTo(webSessionManagerFoundByName); - assertThat(ctx.getBean(ReactorSessionRepository.class)).isNotNull(); + assertThat(this.context.getBean(ReactorSessionRepository.class)).isNotNull(); } @Test public void missingReactorSessionRepositoryBreaksAppContext() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(BadConfig.class); + this.context = new AnnotationConfigApplicationContext(); + this.context.register(BadConfig.class); assertThatExceptionOfType(UnsatisfiedDependencyException.class) - .isThrownBy(ctx::refresh) + .isThrownBy(this.context::refresh) .withMessageContaining("Error creating bean with name 'webSessionManager'") .withMessageContaining("No qualifying bean of type '" + ReactorSessionRepository.class.getCanonicalName()); } @@ -69,22 +78,22 @@ public class SpringWebSessionConfigurationTests { @Test public void defaultSessionIdResolverShouldBeCookieBased() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(GoodConfig.class); - ctx.refresh(); + this.context = new AnnotationConfigApplicationContext(); + this.context.register(GoodConfig.class); + this.context.refresh(); - DefaultWebSessionManager manager = ctx.getBean(DefaultWebSessionManager.class); + DefaultWebSessionManager manager = this.context.getBean(DefaultWebSessionManager.class); assertThat(manager.getSessionIdResolver().getClass()).isAssignableFrom(CookieWebSessionIdResolver.class); } @Test public void providedSessionIdResolverShouldBePickedUpAutomatically() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(OverrideSessionIdResolver.class); - ctx.refresh(); + this.context = new AnnotationConfigApplicationContext(); + this.context.register(OverrideSessionIdResolver.class); + this.context.refresh(); - DefaultWebSessionManager manager = ctx.getBean(DefaultWebSessionManager.class); + DefaultWebSessionManager manager = this.context.getBean(DefaultWebSessionManager.class); assertThat(manager.getSessionIdResolver().getClass()).isAssignableFrom(HeaderWebSessionIdResolver.class); }