Spring MVC参数化测试 - Junit Parameterized
来源:互联网 发布:淘宝 详情批量 编辑:程序博客网 时间:2024/05/29 09:09
在测试类上的@ContextConfiguration注解需要指定spring的配置文件,由于我们使用的是java配置,所以这里传递我们的配置类,记住是spring的配置类,而不是web的配置类(注:web的配置我们是通过模拟的)。
因为controller,component等都是使用注解来,所以我们的@ContextConfiguration使用的是直接来加载的。因此在他的属性中,配置一个loader。同时由于是web的,所以使用AnnotationConfigWebContextLoader。
由于需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
1
2
3
4
@RunWith
(Parameterized.
class
)
@ContextConfiguration
(classes = { WebMVCConfiguration.
class
}, loader = AnnotationConfigWebContextLoader.
class
)
@WebAppConfiguration
public
class
ControllerTest
1
2
3
4
5
6
7
@Controller
public
class
TestController {
@RequestMapping
(value =
"/test"
, method = RequestMethod.GET)
public
@ResponseBody
String test(String name) {
return
name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// parameters
private
String name;
private
String result;
public
ControllerTest(String name, String result) {
this
.name = name;
this
.result = result;
}
@Parameters
public
static
Collection<String[]> data() {
return
Arrays.asList(
new
String[][] { {
"Troy"
,
"Troy"
},
{
"Young"
,
"Young"
} });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private
TestContextManager testContextManager;
private
MockMvc mockMvc;
@Autowired
private
WebApplicationContext wac;
@Before
public
void
setUp()
throws
Exception {
testContextManager =
new
TestContextManager(getClass());
testContextManager.prepareTestInstance(
this
);
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
1
2
3
4
5
6
7
@Test
public
void
test()
throws
Exception {
MvcResult rs = mockMvc.perform(
MockMvcRequestBuilders.get(
"/test?name="
+ name)).andReturn();
assertEquals(
"name is not correct"
, result,
new
String(rs.getResponse()
.getContentAsByteArray()));
}
0 0
- Spring MVC参数化测试 - Junit Parameterized
- JUNIT(Parameterized运行参数化测试)
- unittest参数化parameterized
- junit参数化测试
- junit参数化测试
- spring mvc 使用junit 测试 controller
- junit 4 参数化测试
- spring mvc/mybatis整合,junit测试报BeanCreationException
- spring mvc 引入swagger2.4后,junit测试异常
- spring mvc+junit
- Spring MVC Junit单元测试
- Junit参数化测试的使用方法
- JUnit的理论机制、参数化测试
- JUnit 4的参数化测试
- JUnit 运行参数化测试简单入门
- Junit测试套件及参数化设置
- 使用Junit做参数化测试
- JUnit 测试 Spring
- 调用OpenSSL实现数字签名功能例程(二)
- [九度oj]题目1367:二叉搜索树的后序遍历序列
- Fastjson技术
- LINQ to Entities 不识别方法 System.String get_Item(System.String)
- TMS320VC33 学习笔记 头记
- Spring MVC参数化测试 - Junit Parameterized
- Java垃圾回收机制一 简介–Java Garbage Collection Introduction
- 关于datagirdview
- jQuery Datatable 实用简单实例
- Nginx源码完全注释(9)nginx.c: ngx_get_options .
- Eclipse代码区背景设置
- Pacing
- 字符串与整型的互换
- 学习python(二) 数据结构