Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
eaa95119
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 搜索 >>
提交
eaa95119
编写于
6月 07, 2016
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid ClassCastException in HandlerResultMatchers.methodCall()
上级
5a010011
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
37 addition
and
10 deletion
+37
-10
spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
...mework/test/web/servlet/result/HandlerResultMatchers.java
+23
-10
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java
...ples/standalone/resultmatchers/HandlerAssertionTests.java
+14
-0
未找到文件。
spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
浏览文件 @
eaa95119
...
...
@@ -25,12 +25,14 @@ import org.springframework.test.web.servlet.ResultMatcher;
import
org.springframework.util.ClassUtils
;
import
org.springframework.web.method.HandlerMethod
;
import
org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder
;
import
org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo
;
import
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
;
import
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
assertEquals
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
assertTrue
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
fail
;
/**
* Factory for assertions on the selected handler or handler method.
...
...
@@ -43,6 +45,7 @@ import static org.springframework.test.util.AssertionErrors.assertTrue;
* which is used by default with the Spring MVC Java config and XML namespace.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2
*/
public
class
HandlerResultMatchers
{
...
...
@@ -75,13 +78,13 @@ public class HandlerResultMatchers {
}
/**
* Assert the controller method used to process the request.
The expected
*
method is specified through a "mock" controller method invocation
* similar to {@link MvcUriComponentsBuilder#fromMethodCall(Object)}.
* <p>For example given this controller:
* Assert the controller method used to process the request.
*
<p>The expected method is specified through a "mock" controller method
*
invocation
similar to {@link MvcUriComponentsBuilder#fromMethodCall(Object)}.
* <p>For example
,
given this controller:
* <pre class="code">
* @RestController
*
stat
ic class SimpleController {
*
publ
ic class SimpleController {
*
* @RequestMapping("/")
* public ResponseEntity<Void> handle() {
...
...
@@ -89,19 +92,29 @@ public class HandlerResultMatchers {
* }
* }
* </pre>
* <p>A test can be performed:
* <p>A test that has statically imported {@link MvcUriComponentsBuilder#on}
* can be performed as follows:
* <pre class="code">
* mockMvc.perform(get("/"))
* .andExpect(handler().methodCall(on(SimpleController.class).handle()));
* </pre>
*
* @param obj either the value returned from a "mock" controller invocation
* or the "mock" controller itself after an invocation
*/
public
ResultMatcher
methodCall
(
final
Object
info
)
{
public
ResultMatcher
methodCall
(
final
Object
obj
)
{
return
new
ResultMatcher
()
{
@Override
public
void
match
(
MvcResult
result
)
throws
Exception
{
HandlerMethod
handlerMethod
=
getHandlerMethod
(
result
);
Method
method
=
((
MvcUriComponentsBuilder
.
MethodInvocationInfo
)
info
).
getControllerMethod
();
assertEquals
(
"HandlerMethod"
,
method
,
handlerMethod
.
getMethod
());
if
(!
MethodInvocationInfo
.
class
.
isInstance
(
obj
))
{
fail
(
String
.
format
(
"The supplied object [%s] is not an instance of %s. "
+
"Ensure that you invoke the handler method via MvcUriComponentsBuilder.on()."
,
obj
,
MethodInvocationInfo
.
class
.
getName
()));
}
MethodInvocationInfo
invocationInfo
=
(
MethodInvocationInfo
)
obj
;
Method
expected
=
invocationInfo
.
getControllerMethod
();
Method
actual
=
getHandlerMethod
(
result
).
getMethod
();
assertEquals
(
"Handler method"
,
expected
,
actual
);
}
};
}
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java
浏览文件 @
eaa95119
...
...
@@ -18,7 +18,9 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import
java.lang.reflect.Method
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.web.servlet.MockMvc
;
...
...
@@ -39,11 +41,14 @@ import static org.springframework.web.servlet.mvc.method.annotation.MvcUriCompon
* Examples of expectations on the controller type and controller method.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public
class
HandlerAssertionTests
{
private
final
MockMvc
mockMvc
=
standaloneSetup
(
new
SimpleController
()).
alwaysExpect
(
status
().
isOk
()).
build
();
@Rule
public
final
ExpectedException
exception
=
ExpectedException
.
none
();
@Test
...
...
@@ -52,6 +57,15 @@ public class HandlerAssertionTests {
}
@Test
public
void
methodCallOnNonMock
()
throws
Exception
{
exception
.
expect
(
AssertionError
.
class
);
exception
.
expectMessage
(
"The supplied object [bogus] is not an instance of"
);
exception
.
expectMessage
(
MvcUriComponentsBuilder
.
MethodInvocationInfo
.
class
.
getName
());
exception
.
expectMessage
(
"Ensure that you invoke the handler method via MvcUriComponentsBuilder.on()"
);
this
.
mockMvc
.
perform
(
get
(
"/"
)).
andExpect
(
handler
().
methodCall
(
"bogus"
));
}
@Test
public
void
methodCall
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/"
)).
andExpect
(
handler
().
methodCall
(
on
(
SimpleController
.
class
).
handle
()));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录