开发者学堂课程【RocketMQ 知识精讲与项目实战(第二阶段):Rest 方式测试支付下单和支付回调】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/703/detail/12444
Rest 方式测试支付下单和支付回调
测试 pay-web
在 test 包下创建单元测试类
Name:com.itheima.test.PayWebTest
注入参数,到dubbo中进行取值
@Runwith(SpringRunner.class)
@springBootTest(classes = PaywebApplication.class)
public class PaywlebTest {
@Autowired
private RestTemplate restTemplate;
@Value("
${shop.pay.baseURI}
")
private string baseURI;
@value("
${shop.pay.createPayment}
")
private String createPaymentPath;
@
v
alue("
${shop.pay.
callBackPaymentPath
}
")
private string callBackPaymentPath;
Server port 不能读到9090,读到的是内部-1的值,自己指定 pay.port=9090
继续指定,createPaymentPath 对应 shop.pay.createPayment,callBackPaymentPath 对应shop.pay.callBackPaymentPath,指定后编写两个测试方法,测试方法上加入Test注解,基本测试流程与 PayService一致
@Test
public void createPayment()
{
long orderId = 352
537369385242624
L;
TradePay tradePay = newTradePay();
tradePay.set
O
rder
I
d(orderId);
tradePay. setPayAmount(new BigDecimal( val: 880));
Result result = restTemplate.postForEntity( url: baseURI+
createPaymentPath
,
tradePay
,
Result.
class).getBody();
System.out.println(resul
t)
;
}
通过 restTemplate 测试,发起 http 的请求测试
测试之前确保 orderId 与之前 orderId 是一致的
刷新,更改为原先状态
测试完成后在 Payment 生成一条数据
检查服务是否启动,将未启动的 pay-service 启动
总共启动7个微服务,2个web端,5个服务端
打一个断点
发起请求,结果正确
刷新,支付订单已创建
进行回调测试
发起post请求
@Test
public void c
allBack
Payment()
{
long payId = 3525
42415984402432
L;
long orderId = 35
2537369385242624
L;
TradePay tradePay = new TradePay();
tradePay
.
setPayId(payId);
tradePay
.
set
O
rder
I
d(orderId);
tradePay.setIsPaid(Shop
C
ode.SH
O
P_ORDER_PAY_STATUS_I
S
_PAY.getCode());
restTemplate.postForEntity( url:baseURI+callBackPaymentPath,tradePay ,Result.class).getBody();
System.out.println(result);
}
加一个断点
执行,请求成功
验证执行结果
数据库中 is_paid 动态为2
订单状态改变
订单状态的修改通过发送消息的方式进行处理