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的更多相关文章

  1. Junit mockito 测试Controller层方法有Pageable异常

    1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...

  2. Junit mockito解耦合测试

    Mock测试是单元测试的重要方法之一. 1.相关网址 官网:http://mockito.org/ 项目源码:https://github.com/mockito/mockito api:http:/ ...

  3. Android 单元测试(junit、mockito、robolectric)

    1.运用JUnit4 进行单元测试 首先在工程的 src 文件夹内创建 test 和 test/java 文件夹. 打开工程的 build.gradle(Module:app)文件,添加JUnit4依 ...

  4. Mockito Hello World

    Mockito Hello World 项目配置 IDE是Intellij IDEA,用gradle配置项目. 新建一个Java项目,gradle中需要有这个:   repositories { jc ...

  5. mockito使用心得

    前提:pom引用<dependency> <groupId>junit</groupId> <artifactId>junit</artifact ...

  6. Mock之easymock, powermock, and mockito

    easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...

  7. 用Mockito mock普通的方法

    上面的例子是很理想化的状态,但是在实际的开发中,我们需要经常调用一些依赖特定环境的函数或者调用同事写的代码,而同事仅提供了接口.这个时候就需要利用Mockito来协助我们完成测试. 当然,你可以选择e ...

  8. mock测试框架Mockito

    无论是敏捷开发.持续交付,还是测试驱动开发(TDD)都把单元测试作为实现的基石.随着这些先进的编程开发模式日益深入人心,单元测试如今显得越来越重要了.在敏捷开发.持续交付中要求单元测试一定要快(不能访 ...

  9. Mockito自定义verify参数Matcher

    在TDD开发中,也许我们会遇见对一些重要的无返回值的行为测试,比如在用户的积分DB中增加用户的积分,这个行为对于我们的业务具有重要的价值,所以我们也希望能测试覆盖这部分业务价值.这个时候我们就得使用m ...

  10. Mockito学习资料

    官网:http://mockito.org/ https://dzone.com/refcardz/mockito

随机推荐

  1. 遵循amd规范的require.js(适合浏览器端)

    1. 下载require.js 2. 引用 html <!DOCTYPE html> <html lang="en"> <head> <m ...

  2. 用Task代替TheadPool

    TheadPool的问题 不支持线程的取消.完成.失败通知等交互性操作 不支持线程执行先后次序 using System; using System.Diagnostics; using System ...

  3. 不会用ant打包、部署项目的工程师,不是一个好程序员(测试)

    副标题:利用ant脚本 自动构建svn增量/全量 系统程序升级包 首先请允许我这样说,作为开发或测试,你一定要具备这种本领.你可以手动打包.部署你的工程,但这不是最好的方法.最好的方式就是全自动化的方 ...

  4. MinStack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. Scala 深入浅出实战经典 第40讲:Set、Map、TreeSet、TreeMap操作代码实战

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  6. sqlserver row_number 类似 mysql中 limit 用法

    select * from ( select row_number() over(ORDER BY inspecdate desc,inspectime DESC,itemorder asc ) as ...

  7. [js] js判断浏览器(转)

    (function($, window, document,undefined){ if(!window.browser){ var userAgent = navigator.userAgent.t ...

  8. Window bat expdp 定时任务逻辑备份 定时删除N天前的旧文件

    点击进入:Linux shell crontab expdp 定时任务逻辑备份 定时删除旧文件 首先建一个备份数据库用批处理文件,内容如下: rem expdp sz set sz_file=SZ_% ...

  9. 301 redirect Domain Name using global.asax

    void Application_BeginRequest(object sender, EventArgs e) { if (HttpContext.Current.Request.Url.ToSt ...

  10. java ConcurrentModificationException探究

    当集合结构被修改,会抛出Concurrent Modification Exception. fail-fast会在以下两种情况下抛出ConcurrentModificationException ( ...