欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 测试引擎模拟接口实战

测试引擎模拟接口实战

2024/10/26 0:38:53 来源:https://blog.csdn.net/zhuzhu_YT/article/details/139666528  浏览:    关键词:测试引擎模拟接口实战

在上一章的内容中,我简单介绍了整个微服务的各个子模块,还封装了一些工具类。

当然,若还没完成上次内容的也可以点击右侧的传送门------传送门

EngineApplication

在开发测试引擎模拟接口之前,还需要给xxx-engine创建一个SpringBoot的启动类。

@SpringBootApplication
@EnableTransactionManagement
@EnableFeignClients
@EnableDiscoveryClient
public class EngineApplication {public static void main(String[] args) {SpringApplication.run(EngineApplication.class,args);}
}

那么这里的几个接口需要有以下几个内容:

  • get提交
  • post提交
    • json格式
    • 表单格式
  • 需要以上模拟接口需要支持参数化和随机响应延迟

login

@RequestMapping("/api/v1/test/login_form")@ResponseBodypublic JsonData login(String mail, String pwd){if(mail.startsWith("a")){return JsonData.buildError("账号错误");}return JsonData.buildSuccess("mail=" + mail + "pwd=" + pwd);}

使用form表单形式提交,不可以使用json形式提交。

使用postman来测试,测试结果如下:

pay

 @PostMapping("/api/v1/test/pay_json")@ResponseBodypublic JsonData pay(@RequestBody Map<String,String> map) {String id = map.get("id");String amount = map.get("amount");return JsonData.buildSuccess("id="+id+",amount="+amount);}

这个方法使用的是post提交,且使用json形式进行提交。

继续使用postman进行测试,测试结果如下:

paySleep

这个方法会在上面pay方法上,增加随机睡眠时间的功能。

@PostMapping("/api/v1/test/pay_json_sleep")@ResponseBodypublic JsonData paySleep(@RequestBody Map<String,String> map) {try {int value = RandomUtil.randomInt(1000);TimeUnit.MICROSECONDS.sleep(value);String id = map.get("id");String amount = map.get("amount");return JsonData.buildSuccess("id="+id+",amount="+amount+",sleep="+value);} catch (InterruptedException e) {throw new RuntimeException(e);}}

还是使用postman进行测试,测试结果如下:

queryDetail

@GetMapping("/api/v1/test/query")@ResponseBodypublic JsonData queryDetail(Long id){return JsonData.buildSuccess("id="+id);}

这个方法使用的是get方式提交,并以form表单形式提交。

测试如下:

querySleep

@GetMapping("/api/v1/test/query_sleep")@ResponseBodypublic JsonData querySleep(Long id){try {int value = RandomUtil.randomInt(1000);TimeUnit.MICROSECONDS.sleep(value);return JsonData.buildSuccess("id="+id+",sleep="+value);} catch (InterruptedException e) {throw new RuntimeException(e);}}

这个方法是从以上方法添加了随机睡眠时间的功能

测试如下:

queryError

@GetMapping("/api/v1/test/query_error_code")@ResponseBodypublic JsonData queryError(Long id,  HttpServletResponse response){if(id % 3 == 0){response.setStatus(500);}return JsonData.buildSuccess("id="+id);}
id取模3是0后则http状态码500

测试如下:

以上均为本册测试引擎模拟接口实战

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com