junit 是一个面向于研发人员使用的轻量的测试模块,适合做单元测试。而testng百度后发现更强大,可以做功能测试,但对于我这种RD,貌似junit足沟了呢!

java Mock PowerMock、jmockit 模拟包非常优秀,我目前选型学会一个就不错了,不做评价,我学的powermock,据说jmockit更强大。但是powermock貌似继承了旧的easymock和jmock,所以用的人比较大(个人理解)。

对于junit测试的几个注解

@RunWith

@Before

@Test

@After

powerMock注解

@Mock

@InjectMocks

@RunWith 表示运行方式,@RunWith(JUnit4TestRunner)、@RunWith(SpringRunner.class),@RunWith(PowerMockRunner.class) 三种运行方式,分别在不同的场景中使用。

@Before是 @Test运行之前调用的方法,可以做初始化操作

@Test  是测试用例的单元

@After 执行完测试用例需要执行的清理工作

@Mock  有点类似Autowired注解,而@Mock注解是自动实现模拟对象,而并非Bean实例创建

@InjectMocks 实现对@Mock注解的对象注入到当前对象中,通过MockitoAnnotations.initMocks(this);实现

package aa;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import static org.mockito.Matchers.any;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(PowerMockRunner.class)
@SpringBootTest(classes = MainApplication.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore("javax.management.*")
public class moreTest { @Mock
TService tService;
@InjectMocks
TController controller;
@Autowired
private WebApplicationContext ctx;
private MockMvc mvc; @Before
public void before() throws Exception { MockitoAnnotations.initMocks(this);
//如果简单mock一个请求可以使用这个,但是要模拟注入,还是按下面的方式实现controller比较好
// mvc = MockMvcBuilders.webAppContextSetup(ctx).build();
mvc = MockMvcBuilders.standaloneSetup(controller).build();
// taskPackageService=PowerMockito.mock(TService);
PowerMockito.when(tService.queryMethod(any(ParameClass.class)))
.thenReturn(88);
PowerMockito.whenNew(TService.class).withNoArguments().thenReturn(tService);
} @Test
public void searchTest() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/welcome").header("User-Agent", "xx")
.param("page", "1")
).andExpect(status().isOk()).andDo((MvcResult result) -> {
System.out.println(result.getResponse().getContentAsString());
});
} }

spring boot 测试用例的更多相关文章

  1. Spring Boot 知识图谱

    最近有意重新学习下SpringBoot知识,特地总结了SpringBoot的知识点,对于想要学习的人来说,够用. SpringBoot学习路径 第一部分:了解 Spring Boot Spring B ...

  2. Spring Boot的一个测试用例

    package tk.mybatis.springboot.mapper; import org.junit.Assert; import org.junit.Test; import org.jun ...

  3. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  4. Spring Boot 乐观锁加锁失败 - 使用AOP恢复错误

    之前写了一些辅助工作相关的Spring Boot怎么使用AOP.这里继续正题,怎么减少Spring Boot 乐观锁加锁报错的情况(基本可以解决). 1. 包依赖 spring-boot-starte ...

  5. Spring Boot文档阅读

    原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...

  6. 用Gradle构建Spring Boot项目

    相比起Maven的XML配置方式,Gradle提供了一套简明的DSL用于构建Java项目,使我们就像编写程序一样编写项目构建脚本.本文将从无到有创建一个用Gradle构建的Spring Boot项目, ...

  7. Spring Boot 学习笔记

    参考资料 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ Spring Boot简介 Spring Boot使 ...

  8. springboot(十一):Spring boot中mongodb的使用

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  9. Spring Boot快速入门(三):依赖注入

    原文地址:https://lierabbit.cn/articles/6 spring boot使用依赖注入的方式很简单,只需要给添加相应的注解即可 @Service用于标注业务层组件 @Contro ...

随机推荐

  1. MT【129】常数变易法

    已知数列\(\{x_n\}\)满足\[x_{n+1}=\left(\dfrac 2{n^2}+\dfrac 3n+1\right)x_n+n+1,n\in\mathbf N^*,\]且\(x_1=3\ ...

  2. Android指纹识别API讲解,让你有更好的用户体验

    我发现了一个比较怪的现象.在iPhone上使用十分普遍的指纹认证功能,在Android手机上却鲜有APP使用,我简单观察了一下,发现Android手机上基本上只有支付宝.微信和极少APP支持指纹认证功 ...

  3. number类型转化为string类型

    toString 方法 string = toString(num) 缺点: 不能转化 underfind 和 null 2 String 方法 string = String(num) 可以转化 u ...

  4. 【bzoj2438】 中山市选2011—杀人游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=2438 (题目链接) 题意 n个点的有向图,其中有一个是杀手,每个人成为杀手的概率相同.警察询问一个人 ...

  5. phpredis -- Redis Arrays用法

    Redis Arrays 来自地址:https://github.com/phpredis/phpredis/blob/master/arrays.markdown#readme 扩展原文件array ...

  6. jQuery EasyUI 下拉菜单获取日期,最高年份为当前年份,最低年份为当前年份向前推10年

    http://blog.csdn.net/wangjingjing1014/article/details/16885341 <head><meta http-equiv=" ...

  7. lower_bound/upper_bound example

    http://www.cplusplus.com/reference/algorithm/upper_bound/左闭右开 Return iterator to lower bound Returns ...

  8. Zabbix应用三:Zabbix监控MySQL

    利用Zabbix自带模版监控(Template App MySQL)MySQL服务 一.添加模版: 进入zabbix页面,找到'配置'->'主机'->'模版',点击'链接指示器'后面的'选 ...

  9. C语言基础语法

    #include <stdio.h> int main() { int age; printf("input your age"); scanf("%d&qu ...

  10. python常用模块(3)

    ConfigParser模块学习 ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section),每个节可 ...