我这边在做单元测试,测试代码覆盖率的时候准备模拟一个方法的返回,但是模拟不生效:
工具类:
@Component
@Slf4j
public class routerutil {
public static final string piccycpt = “picc-ycpt";
public static final string URL_GETCOMCODE =“/common/getCwComCode";
public static final string cW_coM_CoDE =“cwComCode";
@Autowired
private PiccRouteProperty piccRouteProperty;
@Autowired
private RestTemplateUtil restTemplate;
public ApiResponse doPostForobj(string value, string url) {
JSONObject param = new JSONObject();
param.put("comCode",value);
return restTemplate.postForobj(param, url, Apiresponse.class);
}
测试类:
public class RouterUtil_bakTest {
private Routerutil routerUtil = new RouterUtilo);
private RestTemplateUtil restTemplate = new RestTemplateUtil();
private ApiResponse apiResponse = new Apiresponse();
@Test
public void doPostForobjTest() {
try {
Map<String,string> testMap = new HashMap<>();
testMap.put("test","test");
apiresponse.setData(testMap);
Mockito.when(restTemplate.postForObj(Mockito.any(JSONObject.class),Mockito.anystringO,Mockito.anyO))).thenreturn(apiResponse);
//Class<?>[] paramTypes =new Class[]{Object.class,string.class,Class.class};
//PowerMockito.stub(PowerMockito.method(RestTemplateUtil.class,"postFor0bj",paramTypes)).toreturn(apiResponse);
//PowerMockito.when(restTemplate.postFor0bj(RestTemplateUtil.class,"postForObj",Mockito.any())).thenReturn(apiresponse);
Apiresponse result = routerUtil.doPostForObj( value: "test", url: "test");
}catch (Exception e){
e.printstackTrace(();
}