Junit Test 常见问题
来源:互联网 发布:知乎 股份 腾讯 编辑:程序博客网 时间:2024/06/16 21:48
anyString()什么时候使用
1.1).比如在controller层,API地址中有映射参数时,那么when的时候(模拟service层调用)就要用到anyString()
eg:
Controller Layer:
@DeleteMapping(value = "/detail/lock/{guid}") @ApiOperation(value = "delete the lock ", notes = "delete the lock.", response =EntlMappingDetailInfoDtoResponse.class) public ResponseEntity<ApiMessage<Object>> deleteObjectLock(@PathVariable String guid) { return new ResponseEntity<>(entitlementMappingService.releaseEntitlementMappingLock(guid, CommonConstants.OBJECT_LOCK_TYPE), HttpStatus.OK); }
Junit Test for Controller Layer:
@Test public void testDeleteObjectLock() throws UnsupportedEncodingException, Exception { String requestURI = "/detail/lock/test"; ApiMessage<Object> message = MessageUtil.getSuccessMessage(anyString()); when(entlMappingService.releaseEntitlementMappingLock(anyString(), CommonConstants.OBJECT_LOCK_TYPE)).thenReturn(message); String response = mockMvc.perform(delete(requestURI)).andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); assertNotNull(response); }
1.2)这里releaseEntitlementMappingLock(argu1,argu2)方法第一个参数由于是从API地址传过来的,所以需要用到anyString()
Controller层delete方法参数是String[]
2.1)eg:
@DeleteMapping(value = "/entitlementmapping") public ResponseEntity<ApiMessage<Object>> deleteEntitlementMapping(@RequestBody String[] guids) { return new ResponseEntity<>(entitlementMappingService.deleteEntitlementMapping(guids), HttpStatus.OK); }
2.2)那么单元测试就应该这样写:
@Test public void testDeleteEntitlementMapping() throws UnsupportedEncodingException, Exception { String requestURI = "/entlmapping/entitlementmapping"; String[] array = { "test" }; JSONArray jsonArray = JSONArray.fromObject(array); String requestBody = jsonArray.toString(); ApiMessage<Object> message = MessageUtil.getSuccessMessage(anyString()); when(entlMappingService.deleteEntitlementMapping(array)).thenReturn(message); String response = mockMvc.perform(delete(requestURI).contentType(MediaType.APPLICATION_JSON).content(requestBody)) .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); assertNotNull(response); }
多层mock问题如何处理
3.1)遇到多层mock问题这样mock,eg:
userId = String.valueOf(httpServletRequest.getSession().getAttribute(CommonConstants.OBJECT_LOCK_USER_ID_KEY));
3.2)思路,先mock:when(httpServletRequest.getSession()).thenReturn(XXX)这个,接下来再mock:
XXX.getAttribute();
3.3)当然这些httpServletRequest需要作为全局变量进行mock
@Mock private HttpServletRequest request; @Mock private HttpSession session;when(request.getSession()).thenReturn(session);
这样就不会出现空指针异常
阅读全文
0 0
- Junit Test 常见问题
- Junit常见问题
- junit test
- junit test
- junit-test
- junit @Test
- [Android开发常见问题-17] Android JUnit Test (两种方法)
- Junit 4 test suit
- android junit test问题
- Junit test EJB3
- JUnit test with Memcached
- android junit test问题
- junit test application代码
- android junit test
- Android JUnit Test
- Junit Test problem
- JUnit test suite 结构
- JUnit多线程Test
- JavaScript event事件
- vsftp跨平台差异性
- 人民币小写转大写
- 主流的LPWAN技术,无非就这几种!
- java基础---java成员初始化过程
- Junit Test 常见问题
- TensorFlow中出现Nan值的解决办法
- element 2.0 上传excel +ssm
- C#导出Excel
- 最新版SublimeText,使用快捷键让在编文件快速在浏览器打开
- C#类和对象(五)——按值和按引用传递参数
- 2017.11.14心得
- 插件平台设计方案
- React Native 调用iOS原生功能—直播