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
随机推荐
- 实现Android Studio JNI开发C/C++使用__android_log_print输出Log
相信很多人在刚开始学习Android JNI编程的时候,需要输出Log,在百度Google搜索的时候都是说需要在Android.mk中加入LOCAL_LDLIBS+= -L$(SYSROOT)/usr ...
- 【干货】个人工作文档节选:XAML MVVM 框架易用性细节优化Tips
1 易用性细节优化 1.1 代码片段 在ViewModel内,会有大量重复性的在Property set中激发 INotifyPropertyChanged.PropertyChanged 事件 ...
- SSD性能对比
SSD性能测试顺序写 16k iops 85061.08顺序写 8k iops 146250.93顺序写 4k iops 239816.69顺序写 2k iops 294540.87顺序写 1k io ...
- 奇怪吸引子---WimolBanlue
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- IOS开发之代码之九宫格
通过UIScrollView展示图片的时候,如果直接向UIScrollView添加UIImageView,在图片数量比较少的时候是没有问题的,但是当我们添加图片数量非常多的时候,会占用大量的内存,我们 ...
- ecshop 加广告出现广告位的宽度值必须在1到1024之间
打开 admin/ad_position.php这个文件,搜索1024,这里你会搜到两个地方 在236行左右 if ($ad_width > 1024 || $ad_width < 1) ...
- ubuntu系统中的VMware 安装win7 Ghost镜像的几个坑
1.ghost镜像安装时要先分区 2.分区后要激活 3.VM(虚拟机安装win7 提示 :units specified don't exist, SHSUCDX can't install)解决方法 ...
- java.util.Properties类
http://trans.blog.51cto.com/503170/110227/ http://soft.chinabyte.com/database/395/12625895.shtml
- Embeding Python & Extending Python with FFPython
Introduction ffpython is a C++ lib, which is to simplify tasks that embed Python and extend Python. ...
- C# vs MySql
MySqlHelper类 /// <summary> ///MySql操作类 /// </summary> public abstract class MySqlHelper ...