Skip to content

Commit c7b2ac7

Browse files
committed
Backported proper updateAccessedAttributes test
Issue: SPR-11738 (cherry picked from commit 1bbc032)
1 parent cbff8b0 commit c7b2ac7

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

spring-web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,11 @@
1717
package org.springframework.web.context.request;
1818

1919
import java.io.Serializable;
20-
2120
import javax.servlet.http.HttpServletRequest;
21+
import javax.servlet.http.HttpSession;
2222

2323
import org.junit.Test;
24+
2425
import org.springframework.mock.web.test.MockHttpServletRequest;
2526
import org.springframework.mock.web.test.MockHttpSession;
2627

@@ -39,23 +40,12 @@ public class ServletRequestAttributesTests {
3940
private static final Serializable VALUE = new Serializable() {
4041
};
4142

43+
4244
@Test(expected = IllegalArgumentException.class)
4345
public void ctorRejectsNullArg() throws Exception {
4446
new ServletRequestAttributes(null);
4547
}
4648

47-
@Test
48-
public void updateAccessedAttributes() throws Exception {
49-
MockHttpSession session = new MockHttpSession();
50-
session.setAttribute(KEY, VALUE);
51-
MockHttpServletRequest request = new MockHttpServletRequest();
52-
request.setSession(session);
53-
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
54-
Object value = attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION);
55-
assertSame(VALUE, value);
56-
attrs.requestCompleted();
57-
}
58-
5949
@Test
6050
public void setRequestScopedAttribute() throws Exception {
6151
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -162,4 +152,20 @@ public void removeSessionScopedAttributeDoesNotForceCreationOfSession() throws E
162152
verify(request).getSession(false);
163153
}
164154

155+
@Test
156+
public void updateAccessedAttributes() throws Exception {
157+
HttpServletRequest request = mock(HttpServletRequest.class);
158+
HttpSession session = mock(HttpSession.class);
159+
when(request.getSession(anyBoolean())).thenReturn(session);
160+
when(session.getAttribute(KEY)).thenReturn(VALUE);
161+
162+
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
163+
assertSame(VALUE, attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION));
164+
attrs.requestCompleted();
165+
166+
verify(session, times(2)).getAttribute(KEY);
167+
verify(session).setAttribute(KEY, VALUE);
168+
verifyNoMoreInteractions(session);
169+
}
170+
165171
}

0 commit comments

Comments
 (0)