涉及静态方法
@PrepareForTest({PosUtils.class})
PowerMockito.mockStatic(PosUtils.class);//不写会报错
Mockito.when(PosUtils.getNowDate()).thenReturn(“20211027”)
Response的创建
String path = “nrcs/data”;
Response response = fileProviderClient.download(path);
测试方法
byte[] bytes = new byte[]{“1”};
Map<String,Collection<String>> headers = new HashMap<>();
Response resp = Response.builder().body(bytes).status(200).reason(“1”).headers(headers).build();
Mockito.when(fileProviderClient.download(Mokito.any())).thenReturn(resp)
通过再Mockito.any()的参数中确定泛型的类型
EsbUtil.getBaseRequest(Mockito.any(),Mockito.any(A.class)).thenReturn(any);
通过第一个参数是第一次调用返回的结果,第二个参数是第二次调用返回的结果Mockito.when(imageService.getFileBarCode(Mockito.any)).thenReturn(“1”,“2”)
如果测试方式涉及到循环,列表循环之后数据会记录内存中
@Value定义的变量
ReflectionTestUtils.setField(testService,"变量名","赋值内容");
User的定义
User userInfo = new UserKeyInfoDto();
((UserKeyInfoDto) userInfo).setUserId("00011");
ObjeBean bean = new ObjBean();
bean.setCode("101");
((UserKeyInfoDto) userInfo).setOrg(bean);
((UserKeyInfoDto) userInfo).setLoginCode("111");
PowerMockito.when(userProviderService.getUserInfo(null,null)).thenReturn((UserKeyInfoDto) userInfo);
void返回的方法
Mockito.doNothing().when(testService).test(Mockito.any())