Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
eefd1c4c
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,发现更多精彩内容 >>
提交
eefd1c4c
编写于
3月 07, 2013
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add context hierarchy tests to Spring MVC Test
Issue: SPR-5613
上级
4c5d7717
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
158 addition
and
10 deletion
+158
-10
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java
...ork/test/web/servlet/samples/context/JavaConfigTests.java
+45
-5
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonController.java
...rk/test/web/servlet/samples/context/PersonController.java
+42
-0
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonDao.java
...framework/test/web/servlet/samples/context/PersonDao.java
+25
-0
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/WebAppResourceTests.java
...test/web/servlet/samples/context/WebAppResourceTests.java
+5
-1
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java
...work/test/web/servlet/samples/context/XmlConfigTests.java
+25
-4
spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/root-context.xml
...amework/test/web/servlet/samples/context/root-context.xml
+12
-0
spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml
...work/test/web/servlet/samples/context/servlet-context.xml
+4
-0
未找到文件。
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java
浏览文件 @
eefd1c4c
...
@@ -16,20 +16,21 @@
...
@@ -16,20 +16,21 @@
package
org.springframework.test.web.servlet.samples.context
;
package
org.springframework.test.web.servlet.samples.context
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
forwardedUrl
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mockito
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextHierarchy
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.web.Person
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.samples.context.JavaConfigTests.RootConfig
;
import
org.springframework.test.web.servlet.samples.context.JavaConfigTests.WebConfig
;
import
org.springframework.test.web.servlet.samples.context.JavaConfigTests.WebConfig
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.context.WebApplicationContext
;
import
org.springframework.web.context.WebApplicationContext
;
...
@@ -42,6 +43,13 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver;
...
@@ -42,6 +43,13 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver;
import
org.springframework.web.servlet.view.tiles3.TilesConfigurer
;
import
org.springframework.web.servlet.view.tiles3.TilesConfigurer
;
import
org.springframework.web.servlet.view.tiles3.TilesView
;
import
org.springframework.web.servlet.view.tiles3.TilesView
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultHandlers
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.*;
import
static
org
.
mockito
.
Mockito
.*;
/**
/**
* Tests with Java configuration.
* Tests with Java configuration.
*
*
...
@@ -50,18 +58,33 @@ import org.springframework.web.servlet.view.tiles3.TilesView;
...
@@ -50,18 +58,33 @@ import org.springframework.web.servlet.view.tiles3.TilesView;
*/
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@WebAppConfiguration
(
"src/test/resources/META-INF/web-resources"
)
@WebAppConfiguration
(
"src/test/resources/META-INF/web-resources"
)
@ContextConfiguration
(
classes
=
WebConfig
.
class
)
@ContextHierarchy
({
@ContextConfiguration
(
classes
=
RootConfig
.
class
),
@ContextConfiguration
(
classes
=
WebConfig
.
class
)
})
public
class
JavaConfigTests
{
public
class
JavaConfigTests
{
@Autowired
@Autowired
private
WebApplicationContext
wac
;
private
WebApplicationContext
wac
;
@Autowired
private
PersonDao
personDao
;
private
MockMvc
mockMvc
;
private
MockMvc
mockMvc
;
@Before
@Before
public
void
setup
()
{
public
void
setup
()
{
this
.
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
this
.
wac
).
build
();
this
.
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
this
.
wac
).
build
();
when
(
this
.
personDao
.
getPerson
(
5L
)).
thenReturn
(
new
Person
(
"Joe"
));
}
@Test
public
void
person
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/person/5"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andDo
(
print
())
.
andExpect
(
status
().
isOk
())
.
andExpect
(
content
().
string
(
"{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"
));
}
}
@Test
@Test
...
@@ -72,10 +95,27 @@ public class JavaConfigTests {
...
@@ -72,10 +95,27 @@ public class JavaConfigTests {
}
}
@Configuration
static
class
RootConfig
{
@Bean
public
PersonDao
personDao
()
{
return
Mockito
.
mock
(
PersonDao
.
class
);
}
}
@Configuration
@Configuration
@EnableWebMvc
@EnableWebMvc
static
class
WebConfig
extends
WebMvcConfigurerAdapter
{
static
class
WebConfig
extends
WebMvcConfigurerAdapter
{
@Autowired
private
RootConfig
rootConfig
;
@Bean
public
PersonController
personController
()
{
return
new
PersonController
(
this
.
rootConfig
.
personDao
());
}
@Override
@Override
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
registry
.
addResourceHandler
(
"/resources/**"
).
addResourceLocations
(
"/resources/"
);
registry
.
addResourceHandler
(
"/resources/**"
).
addResourceLocations
(
"/resources/"
);
...
...
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonController.java
0 → 100644
浏览文件 @
eefd1c4c
/*
* Copyright 2002-2013 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.test.web.servlet.samples.context
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.test.web.Person
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
PersonController
{
private
final
PersonDao
personDao
;
public
PersonController
(
PersonDao
personDao
)
{
this
.
personDao
=
personDao
;
}
@RequestMapping
(
value
=
"/person/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Person
getPerson
(
@PathVariable
long
id
)
{
return
this
.
personDao
.
getPerson
(
id
);
}
}
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/PersonDao.java
0 → 100644
浏览文件 @
eefd1c4c
/*
* Copyright 2002-2013 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.test.web.servlet.samples.context
;
import
org.springframework.test.web.Person
;
public
interface
PersonDao
{
Person
getPerson
(
Long
id
);
}
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/WebAppResourceTests.java
浏览文件 @
eefd1c4c
...
@@ -28,6 +28,7 @@ import org.junit.Test;
...
@@ -28,6 +28,7 @@ import org.junit.Test;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextHierarchy
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MockMvc
;
...
@@ -42,7 +43,10 @@ import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
...
@@ -42,7 +43,10 @@ import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
*/
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@WebAppConfiguration
(
"src/test/resources/META-INF/web-resources"
)
@WebAppConfiguration
(
"src/test/resources/META-INF/web-resources"
)
@ContextConfiguration
(
"servlet-context.xml"
)
@ContextHierarchy
({
@ContextConfiguration
(
"root-context.xml"
),
@ContextConfiguration
(
"servlet-context.xml"
)
})
public
class
WebAppResourceTests
{
public
class
WebAppResourceTests
{
@Autowired
@Autowired
...
...
spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java
浏览文件 @
eefd1c4c
...
@@ -16,20 +16,26 @@
...
@@ -16,20 +16,26 @@
package
org.springframework.test.web.servlet.samples.context
;
package
org.springframework.test.web.servlet.samples.context
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.*;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextHierarchy
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.web.Person
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.context.WebApplicationContext
;
import
org.springframework.web.context.WebApplicationContext
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultHandlers
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.*;
/**
/**
* Tests with XML configuration.
* Tests with XML configuration.
*
*
...
@@ -38,18 +44,33 @@ import org.springframework.web.context.WebApplicationContext;
...
@@ -38,18 +44,33 @@ import org.springframework.web.context.WebApplicationContext;
*/
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@WebAppConfiguration
(
"src/test/resources/META-INF/web-resources"
)
@WebAppConfiguration
(
"src/test/resources/META-INF/web-resources"
)
@ContextConfiguration
(
"servlet-context.xml"
)
@ContextHierarchy
({
@ContextConfiguration
(
"root-context.xml"
),
@ContextConfiguration
(
"servlet-context.xml"
)
})
public
class
XmlConfigTests
{
public
class
XmlConfigTests
{
@Autowired
@Autowired
private
WebApplicationContext
wac
;
private
WebApplicationContext
wac
;
@Autowired
private
PersonDao
personDao
;
private
MockMvc
mockMvc
;
private
MockMvc
mockMvc
;
@Before
@Before
public
void
setup
()
{
public
void
setup
()
{
this
.
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
this
.
wac
).
build
();
this
.
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
this
.
wac
).
build
();
when
(
this
.
personDao
.
getPerson
(
5L
)).
thenReturn
(
new
Person
(
"Joe"
));
}
@Test
public
void
person
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/person/5"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andDo
(
print
())
.
andExpect
(
status
().
isOk
())
.
andExpect
(
content
().
string
(
"{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"
));
}
}
@Test
@Test
...
...
spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/root-context.xml
0 → 100644
浏览文件 @
eefd1c4c
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean
id=
"personDao"
class=
"org.mockito.Mockito"
factory-method=
"mock"
>
<constructor-arg
value=
"org.springframework.test.web.servlet.samples.context.PersonDao"
/>
</bean>
</beans>
\ No newline at end of file
spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml
浏览文件 @
eefd1c4c
...
@@ -7,6 +7,10 @@
...
@@ -7,6 +7,10 @@
<mvc:annotation-driven
/>
<mvc:annotation-driven
/>
<bean
class=
"org.springframework.test.web.servlet.samples.context.PersonController"
>
<constructor-arg
ref=
"personDao"
/>
</bean>
<mvc:view-controller
path=
"/"
view-name=
"home"
/>
<mvc:view-controller
path=
"/"
view-name=
"home"
/>
<mvc:resources
mapping=
"/resources/**"
location=
"/resources/"
/>
<mvc:resources
mapping=
"/resources/**"
location=
"/resources/"
/>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录