简单案例

@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. Qt——线程与定时器

    一.定时器QTimer类 The QTimer class provides repetitive and single-shot timers. The QTimer class provides ...

  2. NewSQL 介绍

    1.CAP: CAP原理:• Consistency(一致性): 数据一致更新,所有数据变动都是同步的• Availability(可用性): 好的响应性能• Partition tolerance( ...

  3. 深入理解JAVA虚拟机JVM

    深入理解JAVA虚拟机JVM Java 虚拟机(Java virtual machine,JVM)是运行 Java 程序必不可少的机制.java之所以能实现一次编写到处执行,也就是因为jVM.原理:编 ...

  4. 创建一个背景透明的UIViewController

    有时候想让UIViewController背景透明,让我们可以看到下层的UI,直接设置它的背景颜色为clearColor(),还是有黑色的默认背景在那里.下面是解决该问题的例子: 在storyboar ...

  5. 前端学习 -- Css -- 伪元素

    :first-letter 表示第一个字符 :first-line 表示文字的第一行 :before 选中元素的最前边,一般该伪类都会结合content一起使用,通过content可以向指定位置添加内 ...

  6. git<commit和分支>

    commit: 在执行提交命令git commit之前,一定要git add要修改的文件,这样才能将更改的内容更新到本地. 在Git 中提交时,会保存一个提交(commit)对象,它包含一个指向暂存内 ...

  7. RF parameter

    There are primarily 3 features which can be tuned to improve the predictive power of the model : 说明: ...

  8. SQL Server 2012安装图解

    SQL Server 2012 Enterprise Edition安装图解... 第一部分:安装前的准备 1.疑问:一个PC上可以安装多个SQL Server数据库么 答案:可以的.每一个安装的时候 ...

  9. OpenStack介绍(一)

    官方网站:http://www.openstack.org/ 介绍 OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开 ...

  10. 使用 Dojo 掌握面向对象开发

    原文出处:Joe Lennon 从头开始学习 Dojo,第 2 部分 使用 Dojo 掌握面向对象开发 什么是面向对象开发? 面向对象编程(Object-Oriented Programming,OO ...