zoukankan      html  css  js  c++  java
  • 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"));
    }

    }
  • 相关阅读:
    Chrome 已经原生支持截图功能,还可以给节点截图!
    【promise| async/await】代码的控制力
    移动端各种分辨率手机屏幕----适配方法集锦
    Web Storage事件无法触发
    【php学习】图片处理三步走
    NYOJ 36 LCS(最长公共子序列)
    NYOJ 252 01串 普通dp
    NYOJ 18 The Triangle 填表法,普通dp
    NYOJ-171 聪明的kk 填表法 普通dp
    NYOJ17 最长单调递增子序列 线性dp
  • 原文地址:https://www.cnblogs.com/cxlings/p/6252693.html
Copyright © 2011-2022 走看看