diff --git a/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java b/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java
index 580ebe1780..0f51d50103 100644
--- a/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java
+++ b/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java
@@ -109,8 +109,10 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
/**
* Set the session to use for the exchange.
- *
This is mutually exclusive with {@link #sessionManager(WebSessionManager)}.
+ *
This method is mutually exclusive with
+ * {@link #sessionManager(WebSessionManager)}.
* @param session the session to use
+ * @see MockWebSession
*/
public Builder session(WebSession session) {
this.sessionManager = exchange -> Mono.just(session);
diff --git a/spring-test/src/main/java/org/springframework/mock/web/server/MockWebSession.java b/spring-test/src/main/java/org/springframework/mock/web/server/MockWebSession.java
new file mode 100644
index 0000000000..5bf53efbda
--- /dev/null
+++ b/spring-test/src/main/java/org/springframework/mock/web/server/MockWebSession.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.mock.web.server;
+
+import java.time.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Map;
+
+import reactor.core.publisher.Mono;
+
+import org.springframework.web.server.WebSession;
+import org.springframework.web.server.session.InMemoryWebSessionStore;
+
+/**
+ * Implementation of {@code WebSession} that delegates to a session instance
+ * obtained via {@link InMemoryWebSessionStore}.
+ *
+ *
This is intended for use with the
+ * {@link MockServerWebExchange.Builder#session(WebSession) session(WebSession)}
+ * method of the {@code MockServerWebExchange} builder, eliminating the need
+ * to use {@code WebSessionManager} or {@code WebSessionStore} altogether.
+ *
+ * @author Rossen Stoyanchev
+ * @since 5.1
+ */
+public class MockWebSession implements WebSession {
+
+ private final WebSession delegate;
+
+
+ public MockWebSession() {
+ this(null);
+ }
+
+ @SuppressWarnings("ConstantConditions")
+ public MockWebSession(Clock clock) {
+ InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore();
+ if (clock != null) {
+ sessionStore.setClock(clock);
+ }
+ this.delegate = sessionStore.createWebSession().block();
+ }
+
+
+ @Override
+ public String getId() {
+ return this.delegate.getId();
+ }
+
+ @Override
+ public Map getAttributes() {
+ return this.delegate.getAttributes();
+ }
+
+ @Override
+ public void start() {
+ this.delegate.start();
+ }
+
+ @Override
+ public boolean isStarted() {
+ return this.delegate.isStarted();
+ }
+
+ @Override
+ public Mono changeSessionId() {
+ return this.delegate.changeSessionId();
+ }
+
+ @Override
+ public Mono invalidate() {
+ return this.delegate.invalidate();
+ }
+
+ @Override
+ public Mono save() {
+ return this.delegate.save();
+ }
+
+ @Override
+ public boolean isExpired() {
+ return this.delegate.isExpired();
+ }
+
+ @Override
+ public Instant getCreationTime() {
+ return this.delegate.getCreationTime();
+ }
+
+ @Override
+ public Instant getLastAccessTime() {
+ return this.delegate.getLastAccessTime();
+ }
+
+ @Override
+ public void setMaxIdleTime(Duration maxIdleTime) {
+ this.delegate.setMaxIdleTime(maxIdleTime);
+ }
+
+ @Override
+ public Duration getMaxIdleTime() {
+ return this.delegate.getMaxIdleTime();
+ }
+
+}
diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/server/MockWebSession.java b/spring-web/src/test/java/org/springframework/mock/web/test/server/MockWebSession.java
new file mode 100644
index 0000000000..3ac6ba09fb
--- /dev/null
+++ b/spring-web/src/test/java/org/springframework/mock/web/test/server/MockWebSession.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.mock.web.test.server;
+
+import java.time.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Map;
+
+import reactor.core.publisher.Mono;
+
+import org.springframework.web.server.WebSession;
+import org.springframework.web.server.session.InMemoryWebSessionStore;
+
+/**
+ * Implementation of {@code WebSession} that delegates to a session instance
+ * obtained via {@link InMemoryWebSessionStore}.
+ *
+ * This is intended for use with the
+ * {@link MockServerWebExchange.Builder#session(WebSession) session(WebSession)}
+ * method of the {@code MockServerWebExchange} builder, eliminating the need
+ * to use {@code WebSessionManager} or {@code WebSessionStore} altogether.
+ *
+ * @author Rossen Stoyanchev
+ * @since 5.1
+ */
+public class MockWebSession implements WebSession {
+
+ private final WebSession delegate;
+
+
+ public MockWebSession() {
+ this(null);
+ }
+
+ @SuppressWarnings("ConstantConditions")
+ public MockWebSession(Clock clock) {
+ InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore();
+ if (clock != null) {
+ sessionStore.setClock(clock);
+ }
+ this.delegate = sessionStore.createWebSession().block();
+ }
+
+
+ @Override
+ public String getId() {
+ return this.delegate.getId();
+ }
+
+ @Override
+ public Map getAttributes() {
+ return this.delegate.getAttributes();
+ }
+
+ @Override
+ public void start() {
+ this.delegate.start();
+ }
+
+ @Override
+ public boolean isStarted() {
+ return this.delegate.isStarted();
+ }
+
+ @Override
+ public Mono changeSessionId() {
+ return this.delegate.changeSessionId();
+ }
+
+ @Override
+ public Mono invalidate() {
+ return this.delegate.invalidate();
+ }
+
+ @Override
+ public Mono save() {
+ return this.delegate.save();
+ }
+
+ @Override
+ public boolean isExpired() {
+ return this.delegate.isExpired();
+ }
+
+ @Override
+ public Instant getCreationTime() {
+ return this.delegate.getCreationTime();
+ }
+
+ @Override
+ public Instant getLastAccessTime() {
+ return this.delegate.getLastAccessTime();
+ }
+
+ @Override
+ public void setMaxIdleTime(Duration maxIdleTime) {
+ this.delegate.setMaxIdleTime(maxIdleTime);
+ }
+
+ @Override
+ public Duration getMaxIdleTime() {
+ return this.delegate.getMaxIdleTime();
+ }
+
+}
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributesHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributesHandlerTests.java
index 598b62c346..25c1808cf4 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributesHandlerTests.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributesHandlerTests.java
@@ -21,11 +21,11 @@ import java.util.HashSet;
import org.junit.Test;
+import org.springframework.mock.web.test.server.MockWebSession;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.server.WebSession;
-import org.springframework.web.server.session.InMemoryWebSessionStore;
import static java.util.Arrays.*;
import static org.junit.Assert.*;
@@ -50,9 +50,7 @@ public class SessionAttributesHandlerTests {
@Test
public void retrieveAttributes() {
- WebSession session = new InMemoryWebSessionStore().createWebSession().block();
- assertNotNull(session);
-
+ WebSession session = new MockWebSession();
session.getAttributes().put("attr1", "value1");
session.getAttributes().put("attr2", "value2");
session.getAttributes().put("attr3", new TestBean());
@@ -72,9 +70,7 @@ public class SessionAttributesHandlerTests {
@Test
public void cleanupAttributes() {
- WebSession session = new InMemoryWebSessionStore().createWebSession().block();
- assertNotNull(session);
-
+ WebSession session = new MockWebSession();
session.getAttributes().put("attr1", "value1");
session.getAttributes().put("attr2", "value2");
session.getAttributes().put("attr3", new TestBean());
@@ -94,14 +90,13 @@ public class SessionAttributesHandlerTests {
@Test
public void storeAttributes() {
- WebSession session = new InMemoryWebSessionStore().createWebSession().block();
- assertNotNull(session);
ModelMap model = new ModelMap();
model.put("attr1", "value1");
model.put("attr2", "value2");
model.put("attr3", new TestBean());
+ WebSession session = new MockWebSession();
sessionAttributesHandler.storeAttributes(session, model);
assertEquals("value1", session.getAttributes().get("attr1"));