mockito
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import net.sf.json.JSONObject; import javax.annotation.Resource; @WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(classes=Application.class)
@ContextConfiguration(locations = {
"classpath:spring/*.xml"
})
public class EmployeeControllerTest { @InjectMocks
private EmployeeController employeeController; @Resource
@Spy
private IEmployeeService employeeService; @Resource
@Spy
private IActionLogsService actionLogsService;
@Resource
@Spy
private RedisTemplate cacheRedisTemplate; private MockMvc mockmvc; @Before
public void before() {
MockitoAnnotations.initMocks(this);
mockmvc = MockMvcBuilders.standaloneSetup(employeeController).build();
} @Test
public void passportLoginTest() throws Exception { ServiceResult2<PsSyncEmployeesVo> serviceresult = new ServiceResult2<>(200, "请求成功");
//Mockito.when(this.employeeService.login(Mockito.anyString(), Mockito.anyString())).thenReturn(serviceresult);
// Mockito.doReturn(serviceresult).when(employeeService).login(Mockito.anyString(), Mockito.anyString());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/pc/login");
builder.param("appId", "");
builder.param("username", "");
builder.param("password", AESUtil.Encrypt("", "", "")); MvcResult mvcResult = mockmvc.perform(builder).andReturn(); String result = mvcResult.getResponse().getContentAsString();
JSONObject object = JSONObject.fromObject(result);
String code = String.valueOf(object.get("code"));
Assert.assertEquals(code, Matchers.eq("200"));
} }
mockito的更多相关文章
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
- Junit mockito解耦合测试
Mock测试是单元测试的重要方法之一. 1.相关网址 官网:http://mockito.org/ 项目源码:https://github.com/mockito/mockito api:http:/ ...
- Android 单元测试(junit、mockito、robolectric)
1.运用JUnit4 进行单元测试 首先在工程的 src 文件夹内创建 test 和 test/java 文件夹. 打开工程的 build.gradle(Module:app)文件,添加JUnit4依 ...
- Mockito Hello World
Mockito Hello World 项目配置 IDE是Intellij IDEA,用gradle配置项目. 新建一个Java项目,gradle中需要有这个: repositories { jc ...
- mockito使用心得
前提:pom引用<dependency> <groupId>junit</groupId> <artifactId>junit</artifact ...
- Mock之easymock, powermock, and mockito
easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...
- 用Mockito mock普通的方法
上面的例子是很理想化的状态,但是在实际的开发中,我们需要经常调用一些依赖特定环境的函数或者调用同事写的代码,而同事仅提供了接口.这个时候就需要利用Mockito来协助我们完成测试. 当然,你可以选择e ...
- mock测试框架Mockito
无论是敏捷开发.持续交付,还是测试驱动开发(TDD)都把单元测试作为实现的基石.随着这些先进的编程开发模式日益深入人心,单元测试如今显得越来越重要了.在敏捷开发.持续交付中要求单元测试一定要快(不能访 ...
- Mockito自定义verify参数Matcher
在TDD开发中,也许我们会遇见对一些重要的无返回值的行为测试,比如在用户的积分DB中增加用户的积分,这个行为对于我们的业务具有重要的价值,所以我们也希望能测试覆盖这部分业务价值.这个时候我们就得使用m ...
- Mockito学习资料
官网:http://mockito.org/ https://dzone.com/refcardz/mockito
随机推荐
- python函数的参数
代码: # coding=utf8 # 可以传入任何个参数 def calc(*numbers): sum = 0 for n in numbers: sum = sum + n * n return ...
- 日常开发中常见的HTTP协议的状态码
301Moved Permanently请求的网页已永久移动到新位置.服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将申请人转到新位置.您应使用此代码告诉 Googlebot 某个 ...
- HTML5手机APP开发入门(1)
HTML5手机APP开发入门(1) 开发框架 Ionicframework V2 + Angular 2 具体内容可以参考一下网站 http://ionicframework.net/ http:// ...
- Spring+hibernate+struts
一.Spring 主要功能:解耦和(对象之间可配置,依赖注入的) 1.概念: 容器:容器可以装载对象,实例化对象,配置对象之间的依赖关系. IOC/DIIOC:Inversion of Control ...
- redis 配置文件 append only file(aof)部分---数据持久化
############################## 仅追加方式 ############################### #默认情况下Redis会异步的将数据导出到磁盘上.这种模式对许 ...
- assertThat用法
一般匹配符1.assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) ); 注释: allOf匹配符表明如果接下来的所有条件必须 ...
- js笔记--1
1.创建一个layer层 var GameLayer = cc.Layer.extend({ _time:null, _ship:null, _backSky:null, // 构造函数 ctor:f ...
- Entity Framework Core 实现读写分离
在之前的版本中我们可用构造函数实现,其实现在的版本也一样,之前来构造连接字符串,现在相似,构造DbContextOptions<T> 代码如下: public SContext(Maste ...
- 新安装的VS的一些设置
古语云:工欲善其事必先利其器 为了方便我们开发,应该设置好VS的一些配置,安装一些辅助插件 1 设置字体和背景等 设置字体为 console 10大小 背景设为护眼颜色 85 90 205 这三个值 ...
- db2 ha create dependency failed 解决
db2diag.log 2014-10-16-23.27.55.009490-240 E31979E444 LEVEL: ErrorPID : 6651 TID : 140508206864160 P ...