简单案例

@RunWith(Parameterized.class)
public class ParameterTest {
// 2.声明变量存放预期值和测试数据
private String firstName;
private String lastName; //3.声明一个返回值 为Collection的公共静态方法,并使用@Parameters进行修饰
@Parameterized.Parameters
public static List<Object[]> param() {
// 这里我给出两个测试用例
return Arrays.asList(new Object[][]{{"Mike", "Black"}, {"Cilcln", "Smith"}});
} //4.为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
public ParameterTest(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
} // 5. 进行测试,发现它会将所有的测试用例测试一遍
@Test
public void test() {
String name = firstName + " " + lastName;
System.out.println(name);
assertThat("Mike Black", is(name));
} }

参数化在 测试controller 中的应用

@RunWith(Parameterized.class)
@SpringBootTest
public class LearnController14Test { @Autowired
private WebApplicationContext wac; private MockMvc mvc; private MockHttpSession session; public Long id;
public String title; public LearnController14Test(Long id, String title) {
super();
this.id = id;
this.title = title;
} /**
* 这些参数,都会测试一遍
*
* @return
*/
@Parameterized.Parameters
public static List<Object[]> data() {
return Arrays.asList(new Object[][]{{999L, "Black"}, {997L, "Smith"}});
} @Before
public void setupMockMvc() throws Exception {
//借助TestContextManager来实现上下文注入
TestContextManager testContextManager = new TestContextManager(getClass());
testContextManager.prepareTestInstance(this); //初始化MockMvc对象
mvc = MockMvcBuilders.webAppContextSetup(wac).build(); //构建session
session = new MockHttpSession();
User user = new User("root", "root");
//拦截器那边会判断用户是否登录,所以这里注入一个用户
session.setAttribute("user", user);
} /**
* 获取教程测试用例
* <p>
* get 请求
*
* @throws Exception
*/
@Test
public void qryLearn() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/learn/resource/" + id + "?title=" + title)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.session(session)
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print());
} }



spring-boot-单元测试参数数的更多相关文章

  1. Spring Boot单元测试(Mock)

    Spring Boot单元测试(Mock) Java个人学习心得 2017-08-12 16:07 Mock 单元测试的重要性就不多说了,我这边的工程一般都是Spring Boot+Mybatis(详 ...

  2. Spring Boot 单元测试示例

    Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的单元测试. 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启 ...

  3. 48. spring boot单元测试restfull API【从零开始学Spring Boot】

    回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  4. Kotlin + Spring Boot 请求参数验证

    编写 Web 应用程序的时候,经常要做的事就是要对前端传回的数据进行简单的验证,比如是否非空.字符长度是否满足要求,邮箱格式是否正确等等.在 Spring Boot 中,可以使用 Bean Valid ...

  5. Spring boot 配置文件参数映射到配置类属性

    [参考文章]:SpringBoot之@EnableConfigurationProperties分析 [参考文章]:在Spring Boot中使用 @ConfigurationProperties 注 ...

  6. Spring Boot 单元测试详解+实战教程

    Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring-boot-test:支持测试的核心内容. spring-b ...

  7. spring boot 单元测试,如何使用profile

    一.问题概述 spring boot项目.单元测试的时候,我发现,总是会使用application.properties的内容,而该文件里,一般是我的开发时候的配置. 比如上图中,dev是开发配置,p ...

  8. Java程序员的日常—— Spring Boot单元测试

    关于Spring boot 之前没有用Spring的时候是用的MockMvc,做接口层的测试,原理上就是加载applicationContext.xml文件,然后模拟启动各种mybatis\连接池等等 ...

  9. spring boot 输入参数统一校验

    1 引入spring boot validate    maven 依赖 <!-- 验证 --> <dependency> <groupId>org.hiberna ...

  10. Spring Boot 构造器参数绑定,越来越强大了!

    在之前的文章:Spring Boot读取配置的几种方式,我介绍到 Spring Boot 中基于 Java Bean 的参数绑定,在一个 Java Bean 类上用 @ConfigurationPro ...

随机推荐

  1. how to get iframe dom in js

    how to get iframe dom in js https://stackoverflow.com/questions/3999101/get-iframes-document-from-ja ...

  2. Windows系统下搭建Appium自动化测试框架

    简介 一种开源的测试框架(http://appium.io/) 能够用来测试原生Android/iOS应用.混合应用以及webapp 通过webdriver协议来操作应用,其核心是一个web服务器,接 ...

  3. Visual Categorization with Bags of Keypoints

    1.Introduction and backgrounds 作为本周的论文之一,这是一篇bag of features的基本文章之一,主要了解其中的基本思路,以及用到的基本技术,尽量使得细节更加清楚 ...

  4. JS的原生函数

    常用的原生函数有: String() Number() Boolean() Array() Object() Function() RegExp() Date() Error() Symbol() 1 ...

  5. [Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法(转)

    [Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法http://blog.csdn.net/qiujuer/article/details/ ...

  6. 解题:TJOI 2015 组合数学

    题面 通过这个题理解了一下反链的概念,更新在图论知识点里了 每个点向右和下连边可以建出一张图,这个题事实上是让我们求图的最小链覆盖.Dilworth定理告诉我们,最小链覆盖等于最长反链(反链:DAG中 ...

  7. bzoj 1212: [HNOI2004]L语言 AC自动机+状压

    为什么这道题网上所有题解写的都是N*Len的trie树的暴力啊,4E的复杂度... 为什么暴力还跑这么快啊TAT.. 有一个O(Len)的做法就是先把AC自动机建出来,因为每个字典串的长度很小,所以我 ...

  8. socket利用多线程实现一对多通信

    1.服务器端:socket()建立套接字,绑定(bind)并监听(listen),用accept()等待客户端连接.将accept()写入死循环,每次连接一个客户端,开一个线程. 2.一般情况下建立s ...

  9. Hadoop生态圈-hive编写自定义函数

    Hadoop生态圈-hive编写自定义函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  10. Showbo.js弹窗实现(jquery)

    一.搭建环境 下载showBo.js和showBo.css 下载链接:https://pan.baidu.com/s/1iUUlKXFNXCBEvBnds4ECIA  密码:its4 显示效果图: 二 ...