Java Mockito:如何使服务调用抛出异常
我的类中有Hystrix命令需要测试。我可以模拟所有的代码,除了备用代码。要执行回退,我需要使我的hystrix包装方法抛出超时异常。我不知道该怎么做。有人能帮我吗?我尝试在测试类上使用@Enablecircuitbreaker打开电路,但没有调用任何Hystrix异常:( 在这个测试中,由hystrix包装的命令在Java Mockito:如何使服务调用抛出异常,java,mocking,mockito,hystrix,Java,Mocking,Mockito,Hystrix,我的类中有Hystrix命令需要测试。我可以模拟所有的代码,除了备用代码。要执行回退,我需要使我的hystrix包装方法抛出超时异常。我不知道该怎么做。有人能帮我吗?我尝试在测试类上使用@Enablecircuitbreaker打开电路,但没有调用任何Hystrix异常:( 在这个测试中,由hystrix包装的命令在 GetCustomerAccountResponseType responseType = aiaUtilities .fetchCustomerAccount
GetCustomerAccountResponseType responseType = aiaUtilities
.fetchCustomerAccountDetails(accountNumber);
具有hystrix命令和相应回退的AIA实用程序的代码为
@HystrixCommand(commandKey = "AIAClientCommand", fallbackMethod = "aiaClientCommandFallback")
public GetCustomerAccountResponseType fetchCustomerAccountDetails(String accountNumber)
throws DataAccessException {
GetCustomerAccountResponseType response;
try {
if (generalUtil.isObjectEmpty(authHeader)) {
authHeader = iamUtilities.createAuthHeaderAndRenewOfflineTicket();
}
factory = getFactory();
request = getRequest();
accountNumberType = getAccountNumberType();
accountNumberType.setValue(accountNumber);
request.setCustomerAccountNumber(accountNumberType);
request.setSourceId(Constants.VAL_QUICKBASE_SOURCE_AIA);
serviceClass = getServiceClass();
service = getService();
provider = getProvider();;
provider.getRequestContext().put("Authorization", authHeader);
provider.getRequestContext().replace("SOAPAction", "fetchCustomerAccount");
provider.getRequestContext().put("Content-Type", "text/xml");
response = service.fetchCustomerAccount(request);
} catch (DataAccessException e) {
throw e;
}
catch (Exception e) {
if(e instanceof HystrixRuntimeException && e.getCause() instanceof TimeoutException) {
DataAccessException dataAccessException = (DataAccessException) ((HystrixRuntimeException) e)
.getFallbackException().getCause();
throw new DataAccessException(dataAccessException.getErrorCode(),
"Exception in validateLicense.fetchCustomerAccountDetails::" + e.getMessage(),e);
}
else
throw new DataAccessException(Constants.ERRCODE_AIA_EXCEPTION,
"Exception in validateLicense.fetchCustomerAccountDetails:::" + e.toString(), e);
}
return response;
}
private GetCustomerAccountResponseType aiaClientCommandFallback(String accountNumber, Throwable e)
throws DataAccessException {
logger.error("Inside AIAClientCommandFallback : Error is ::" + e.toString());
if(e instanceof HystrixTimeoutException)
throw new DataAccessException(Constants.ERRCODE_AIA_QUERY_TIMED_OUT,
"Exception in AIAClientCommandFallback::" + e.toString(),e);
else if(e instanceof DataAccessException)
throw (DataAccessException)e;
else
throw new DataAccessException(Constants.ERRCODE_AIA_EXCEPTION,
"Inside AIAClientCommandFallback : Error is ::" + e.toString(), e);
}
不要在模拟的fetchCustomerAccount中返回某个内容,只需通过
然后抛出一个异常即可:
Mockito.when(service.fetchCustomerAccount(any(GetCustomerAccountType.class))).thenThrow(new RuntimeException("Timeout"));
不要在模拟的fetchCustomerAccount中返回某个内容,只需通过然后抛出一个异常即可:
Mockito.when(service.fetchCustomerAccount(any(GetCustomerAccountType.class))).thenThrow(new RuntimeException("Timeout"));
由hystrix包装的调用由GetCustomerAccountResponseType responseType=aiaUtilities.fetchCustomerAccountDetails(accountNumber)执行;好的,因此您需要模拟aiaUtilities
并在那里抛出异常?如Mockito.when(aiaUtilities.fetchCustomerAccountDetails(…)。然后抛出(…)
但是,测试是针对AIAIUtilities本身的。好吧……但是如果AIAIUtilities使用MDMConnectorService服务,那么第一个解决方案应该可以工作,不是吗?否则,对MDMConnectorService的模仿是为了什么?我不能让外部服务抛出它们不打算抛出的异常。我的代码AIAIAIUtilities.fetchCustomerAccountDetails应该抛出异常以调用回退。由hystrix包装的调用由GetCustomerAccountResponseType responseType=aiaUtilities.fetchCustomerAccountDetails(accountNumber)执行;好的,因此您需要模拟aiaUtilities
并在那里抛出异常?如Mockito.when(aiaUtilities.fetchCustomerAccountDetails(..).然后抛出(…)
但是,测试是针对AIAIUtilities本身的。好吧……但是如果AIAIUtilities使用MDMConnectorService服务,那么第一个解决方案应该可以工作,不是吗?否则,对MDMConnectorService的模仿是为了什么?我不能让外部服务抛出它们不打算抛出的异常。我的代码AIAIAIUtilities.fetchCustomerAccountDetails应该抛出异常以调用回退。