Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
51eccf55
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
51eccf55
编写于
3月 10, 2016
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish ServletTestExecutionListener[Tests]
上级
bb590db3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
60 addition
and
50 deletion
+60
-50
spring-test/src/main/java/org/springframework/test/context/web/ServletTestExecutionListener.java
...mework/test/context/web/ServletTestExecutionListener.java
+8
-5
spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java
...k/test/context/web/ServletTestExecutionListenerTests.java
+52
-45
未找到文件。
spring-test/src/main/java/org/springframework/test/context/web/ServletTestExecutionListener.java
浏览文件 @
51eccf55
...
...
@@ -61,6 +61,7 @@ import org.springframework.web.context.request.ServletWebRequest;
* See the Javadoc for individual methods in this class for details.
*
* @author Sam Brannen
* @author Phillip Webb
* @since 3.2
*/
public
class
ServletTestExecutionListener
extends
AbstractTestExecutionListener
{
...
...
@@ -99,14 +100,16 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
ServletTestExecutionListener
.
class
,
"createdByTheTestContextFramework"
);
/**
* Attribute name for a {@link TestContext} attribute which indicates that
* th
at the {@link ServletTestExecutionListener} should be activated. When
*
not specified
activation occurs when the {@linkplain TestContext#getTestClass()
* Attribute name for a {@link TestContext} attribute which indicates that
that
* th
e {@code ServletTestExecutionListener} should be activated. When not set to
*
{@code true},
activation occurs when the {@linkplain TestContext#getTestClass()
* test class} is annotated with {@link WebAppConfiguration @WebAppConfiguration}.
* @since 4.4
*
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
* @since 4.3
*/
public
static
final
String
ACTIVATE_LISTENER
=
Conventions
.
getQualifiedAttributeName
(
ServletTestExecutionListener
.
class
,
"webApplicationConfiguration
"
);
ServletTestExecutionListener
.
class
,
"activateListener
"
);
private
static
final
Log
logger
=
LogFactory
.
getLog
(
ServletTestExecutionListener
.
class
);
...
...
spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java
浏览文件 @
51eccf55
...
...
@@ -38,11 +38,12 @@ import static org.springframework.test.context.web.ServletTestExecutionListener.
* Unit tests for {@link ServletTestExecutionListener}.
*
* @author Sam Brannen
* @author Phillip Webb
* @since 3.2.6
*/
public
class
ServletTestExecutionListenerTests
{
private
static
final
String
SET_UP_OUTSIDE_OF_STEL
=
"
SET_UP_OUTSIDE_OF_STEL
"
;
private
static
final
String
SET_UP_OUTSIDE_OF_STEL
=
"
setUpOutsideOfStel
"
;
private
final
WebApplicationContext
wac
=
mock
(
WebApplicationContext
.
class
);
private
final
MockServletContext
mockServletContext
=
new
MockServletContext
();
...
...
@@ -50,30 +51,6 @@ public class ServletTestExecutionListenerTests {
private
final
ServletTestExecutionListener
listener
=
new
ServletTestExecutionListener
();
private
void
assertAttributesAvailable
()
{
assertNotNull
(
"request attributes should be available"
,
RequestContextHolder
.
getRequestAttributes
());
}
private
void
assertAttributesNotAvailable
()
{
assertNull
(
"request attributes should not be available"
,
RequestContextHolder
.
getRequestAttributes
());
}
private
void
assertAttributeExists
()
{
RequestAttributes
requestAttributes
=
RequestContextHolder
.
getRequestAttributes
();
assertNotNull
(
"request attributes should exist"
,
requestAttributes
);
Object
setUpOutsideOfStel
=
requestAttributes
.
getAttribute
(
SET_UP_OUTSIDE_OF_STEL
,
RequestAttributes
.
SCOPE_REQUEST
);
assertNotNull
(
SET_UP_OUTSIDE_OF_STEL
+
" should exist as a request attribute"
,
setUpOutsideOfStel
);
}
private
void
assertAttributeDoesNotExist
()
{
RequestAttributes
requestAttributes
=
RequestContextHolder
.
getRequestAttributes
();
assertNotNull
(
"request attributes should exist"
,
requestAttributes
);
Object
setUpOutsideOfStel
=
requestAttributes
.
getAttribute
(
SET_UP_OUTSIDE_OF_STEL
,
RequestAttributes
.
SCOPE_REQUEST
);
assertNull
(
SET_UP_OUTSIDE_OF_STEL
+
" should NOT exist as a request attribute"
,
setUpOutsideOfStel
);
}
@Before
public
void
setUp
()
{
given
(
wac
.
getServletContext
()).
willReturn
(
mockServletContext
);
...
...
@@ -86,7 +63,7 @@ public class ServletTestExecutionListenerTests {
request
.
setAttribute
(
SET_UP_OUTSIDE_OF_STEL
,
"true"
);
RequestContextHolder
.
setRequestAttributes
(
servletWebRequest
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
}
@Test
...
...
@@ -95,16 +72,16 @@ public class ServletTestExecutionListenerTests {
given
(
testContext
.
getApplicationContext
()).
willReturn
(
mock
(
ApplicationContext
.
class
));
listener
.
beforeTestClass
(
testContext
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
listener
.
prepareTestInstance
(
testContext
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
listener
.
beforeTestMethod
(
testContext
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
listener
.
afterTestMethod
(
testContext
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
}
@Test
...
...
@@ -112,22 +89,22 @@ public class ServletTestExecutionListenerTests {
BDDMockito
.<
Class
<?>>
given
(
testContext
.
getTestClass
()).
willReturn
(
LegacyWebTestCase
.
class
);
RequestContextHolder
.
resetRequestAttributes
();
assert
AttributesNotAvailable
();
assert
RequestAttributesDoNotExist
();
listener
.
beforeTestClass
(
testContext
);
listener
.
prepareTestInstance
(
testContext
);
assert
AttributesNotAvailable
();
assert
RequestAttributesDoNotExist
();
verify
(
testContext
,
times
(
0
)).
setAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
given
(
testContext
.
getAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
)).
willReturn
(
null
);
listener
.
beforeTestMethod
(
testContext
);
assert
AttributesNotAvailable
();
assert
RequestAttributesDoNotExist
();
verify
(
testContext
,
times
(
0
)).
setAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
listener
.
afterTestMethod
(
testContext
);
verify
(
testContext
,
times
(
1
)).
removeAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
);
assert
AttributesNotAvailable
();
assert
RequestAttributesDoNotExist
();
}
@Test
...
...
@@ -135,21 +112,21 @@ public class ServletTestExecutionListenerTests {
BDDMockito
.<
Class
<?>>
given
(
testContext
.
getTestClass
()).
willReturn
(
LegacyWebTestCase
.
class
);
listener
.
beforeTestClass
(
testContext
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
listener
.
prepareTestInstance
(
testContext
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
verify
(
testContext
,
times
(
0
)).
setAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
given
(
testContext
.
getAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
)).
willReturn
(
null
);
listener
.
beforeTestMethod
(
testContext
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
verify
(
testContext
,
times
(
0
)).
setAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
given
(
testContext
.
getAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
)).
willReturn
(
null
);
listener
.
afterTestMethod
(
testContext
);
verify
(
testContext
,
times
(
1
)).
removeAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
);
assertAttributeExists
();
assert
SetUpOutsideOfStel
AttributeExists
();
}
@Test
...
...
@@ -158,7 +135,7 @@ public class ServletTestExecutionListenerTests {
RequestContextHolder
.
resetRequestAttributes
();
listener
.
beforeTestClass
(
testContext
);
assert
AttributesNotAvailable
();
assert
RequestAttributesDoNotExist
();
assertWebAppConfigTestCase
();
}
...
...
@@ -168,40 +145,70 @@ public class ServletTestExecutionListenerTests {
BDDMockito
.<
Class
<?>>
given
(
testContext
.
getTestClass
()).
willReturn
(
AtWebAppConfigWebTestCase
.
class
);
listener
.
beforeTestClass
(
testContext
);
assert
AttributesAvailable
();
assert
RequestAttributesExist
();
assertWebAppConfigTestCase
();
}
/**
* @since 4.3
*/
@Test
public
void
activate
AttributeAppConfigTestCase
WithoutExistingRequestAttributes
()
throws
Exception
{
public
void
activate
Listener
WithoutExistingRequestAttributes
()
throws
Exception
{
BDDMockito
.<
Class
<?>>
given
(
testContext
.
getTestClass
()).
willReturn
(
NoAtWebAppConfigWebTestCase
.
class
);
given
(
testContext
.
getAttribute
(
ServletTestExecutionListener
.
ACTIVATE_LISTENER
)).
willReturn
(
true
);
RequestContextHolder
.
resetRequestAttributes
();
listener
.
beforeTestClass
(
testContext
);
assert
AttributesNotAvailable
();
assert
RequestAttributesDoNotExist
();
assertWebAppConfigTestCase
();
}
private
RequestAttributes
assertRequestAttributesExist
()
{
RequestAttributes
requestAttributes
=
RequestContextHolder
.
getRequestAttributes
();
assertNotNull
(
"request attributes should exist"
,
requestAttributes
);
return
requestAttributes
;
}
private
void
assertRequestAttributesDoNotExist
()
{
assertNull
(
"request attributes should not not exist"
,
RequestContextHolder
.
getRequestAttributes
());
}
private
void
assertSetUpOutsideOfStelAttributeExists
()
{
RequestAttributes
requestAttributes
=
assertRequestAttributesExist
();
Object
setUpOutsideOfStel
=
requestAttributes
.
getAttribute
(
SET_UP_OUTSIDE_OF_STEL
,
RequestAttributes
.
SCOPE_REQUEST
);
assertNotNull
(
SET_UP_OUTSIDE_OF_STEL
+
" should exist as a request attribute"
,
setUpOutsideOfStel
);
}
private
void
assertSetUpOutsideOfStelAttributeDoesNotExist
()
{
RequestAttributes
requestAttributes
=
assertRequestAttributesExist
();
Object
setUpOutsideOfStel
=
requestAttributes
.
getAttribute
(
SET_UP_OUTSIDE_OF_STEL
,
RequestAttributes
.
SCOPE_REQUEST
);
assertNull
(
SET_UP_OUTSIDE_OF_STEL
+
" should NOT exist as a request attribute"
,
setUpOutsideOfStel
);
}
private
void
assertWebAppConfigTestCase
()
throws
Exception
{
listener
.
prepareTestInstance
(
testContext
);
assertAttributeDoesNotExist
();
assertRequestAttributesExist
();
assertSetUpOutsideOfStelAttributeDoesNotExist
();
verify
(
testContext
,
times
(
1
)).
setAttribute
(
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
verify
(
testContext
,
times
(
1
)).
setAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
given
(
testContext
.
getAttribute
(
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
)).
willReturn
(
Boolean
.
TRUE
);
given
(
testContext
.
getAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
)).
willReturn
(
Boolean
.
TRUE
);
listener
.
beforeTestMethod
(
testContext
);
assertAttributeDoesNotExist
();
assertRequestAttributesExist
();
assertSetUpOutsideOfStelAttributeDoesNotExist
();
verify
(
testContext
,
times
(
1
)).
setAttribute
(
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
verify
(
testContext
,
times
(
1
)).
setAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
,
Boolean
.
TRUE
);
listener
.
afterTestMethod
(
testContext
);
verify
(
testContext
).
removeAttribute
(
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
);
verify
(
testContext
).
removeAttribute
(
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
);
assert
AttributesNotAvailable
();
assert
RequestAttributesDoNotExist
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录