1
0
mirror of synced 2026-10-08 07:46:22 +08:00

测试:补充请求体读取和余额查询异常路径回归测试

This commit is contained in:
Binary Wang
2026-09-13 11:04:06 +08:00
parent 0c7b6713a8
commit 9d848a5649
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.ecommerce.FundBalanceResult;
import com.github.binarywang.wxpay.bean.ecommerce.enums.SpAccountTypeEnum;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EcommerceService;
import com.github.binarywang.wxpay.service.WxPayService;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
public class PartnerTransferServiceImplExceptionTest {
@Test
public void testSpDayEndBalancePreservesPayException() {
WxPayException failure = new WxPayException("balance query failed");
WxPayService payService = new WxPayServiceImpl() {
@Override
public EcommerceService getEcommerceService() {
return new EcommerceServiceImpl(this) {
@Override
public FundBalanceResult spDayEndBalance(SpAccountTypeEnum accountType, String date)
throws WxPayException {
assertEquals(accountType, SpAccountTypeEnum.BASIC);
assertEquals(date, "2020-09-11");
throw failure;
}
};
}
};
assertThatThrownBy(() -> new PartnerTransferServiceImpl(payService)
.spDayEndBalance(SpAccountTypeEnum.BASIC, "2020-09-11"))
.isInstanceOf(WxRuntimeException.class).hasCause(failure);
}
}

View File

@@ -0,0 +1,85 @@
package com.github.binarywang.wxpay.util;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.testng.annotations.Test;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Proxy;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertTrue;
public class RequestUtilsTest {
@Test
public void testGetReaderFailure() {
IOException failure = new IOException("getReader failed");
HttpServletRequest request = (HttpServletRequest) Proxy.newProxyInstance(
HttpServletRequest.class.getClassLoader(), new Class<?>[]{HttpServletRequest.class},
(proxy, method, args) -> {
if ("getReader".equals(method.getName())) {
throw failure;
}
throw new UnsupportedOperationException(method.getName());
});
assertThatThrownBy(() -> RequestUtils.readData(request))
.isInstanceOf(WxRuntimeException.class).hasCause(failure);
}
@Test
public void testPartialReadFailureClosesReader() {
IOException failure = new IOException("read failed");
boolean[] closed = {false};
BufferedReader reader = new BufferedReader(new StringReader("")) {
private int reads;
@Override
public String readLine() throws IOException {
if (reads++ == 0) {
return "partial body";
}
throw failure;
}
@Override
public void close() throws IOException {
closed[0] = true;
super.close();
}
};
assertThatThrownBy(() -> RequestUtils.readData(requestWithReader(reader)))
.isInstanceOf(WxRuntimeException.class).hasCause(failure);
assertTrue(closed[0], "Reader must be closed after a read failure");
}
@Test
public void testCloseFailure() {
IOException failure = new IOException("close failed");
BufferedReader reader = new BufferedReader(new StringReader("complete body")) {
@Override
public void close() throws IOException {
super.close();
throw failure;
}
};
assertThatThrownBy(() -> RequestUtils.readData(requestWithReader(reader)))
.isInstanceOf(WxRuntimeException.class).hasCause(failure);
}
private HttpServletRequest requestWithReader(BufferedReader reader) {
return (HttpServletRequest) Proxy.newProxyInstance(
HttpServletRequest.class.getClassLoader(), new Class<?>[]{HttpServletRequest.class},
(proxy, method, args) -> {
if ("getReader".equals(method.getName())) {
return reader;
}
throw new UnsupportedOperationException(method.getName());
});
}
}