Reputation: 699
I have a function in controller class that calls a Rest Easy web service which returns a response. I need to unit test that particular function.
public void createOrderRequest(OrderModel orderModel, ResourceBundle resourceBundle, AspectModel aspectModel) {
try {
LOG.debug("Creating order request");
OrderReq orderRequest = new OrderReq();
orderRequest.getHeader().setDestination("http://localhost:8080/middleware/ws/services/txn/getReport");
orderRequest.setUserId("abc");
OrderResp response = (OrderResp) OrderService.getInstance().getOrderService().sendRequest(orderRequest);
if (response.getHeader().getErrorCode() == ErrorCode.SUCCESS.getErrorCode()) {
LOG.debug("Successfully send order request");
orderModel.setErrorDescription("Order successfully sent");
aspectModel.set(orderModel);
}
} catch (Exception ex) {
LOG.error("Error while sending order request: " + ex.getMessage());
}
}
I want to mock the order request object OrderReq
and response object OrderResp
. My intention is to create mock response for the rest easy web service request. How can I achieve it ?
Upvotes: 0
Views: 756
Reputation: 328770
The most simple way is to move the object creation into a help method which you can override in a test:
public void createOrderRequest(OrderModel orderModel, ResourceBundle resourceBundle, AspectModel aspectModel) {
try {
LOG.debug("Creating order request");
OrderReq orderRequest = createOrderReq();
....
}
}
/*test*/ OrderReq createOrderReq() { return new OrderReq(); }
Using package private (default) visibility, a test can override the method (since they are in the same package).
Alternatively, you can create a factory and inject that.
Upvotes: 1