Feign:如何在Spring中捕获API错误状态代码

时间:2019-04-30 08:39:15

标签: spring-boot web-api-testing spring-cloud-feign

我正在使用srping框架进行API测试。对于API返回错误代码(例如400,401,403)的否定测试用例。假装总是抛出异常

有什么方法可以捕获实际API响应,而不是假异常并断言它。

@FeignClient(name = "StockTransferProxy", url = "${app.url}")
public interface `StockTransferProxy` {

@PutMapping(value = "${api.scanItems}")
ScannedItemResponse scanItems(@RequestHeader(HttpHeaders.AUTHORIZATION)String auth,
                              @RequestParam("transferId") String transferId,
                              @RequestBody ScannedItemRequest body);
 }

测试用例文件

@EnableWebTests
public class StockTransfer extends CoreTest {

private static final Logger log = LoggerFactory.getLogger(StockTransfer.class);

private String auth = "...";

@Autowired
private StockTransferProxy stockTransferProxy;


@Test
public void stockTransfer_1() {
ScannedItemResponse scannedItem = stockTransferProxy.scanItems(auth, transferId, new ScannedItemRequest("010001707340051"));}

有关获取实际http状态代码和消息的任何帮助

0 个答案:

没有答案