[英]Mockito is not mocking but making actual call to the third party service
我正在嘗試模擬正在調用第三方服務的對象,但是在執行測試用例時未使用我的模擬類。 相反,它將實際呼叫第三方服務。 有人知道為什么嗎?
我的when()then()
有效。
這是我的集成測試課程:
public class CheckoutStepsAddressITest extends AbstractITest {
//Class to be tested
@Autowired private CheckoutStepsAddressUtil checkoutStepsAddressUtil;
//Dependencies (will be mocked)
private CustomerService customerService;
//Test data
private AddressResponse addressResponse;
private CheckoutAddressView checkoutAddressView;
private AddressView addressView;
@Before
public void setup() {
addressResponse = createAddressResponse();
customerService = mock(CustomerService.class);
checkoutAddressView = new CheckoutAddressView();
checkoutAddressView.setNewAddress(createAddressView());
addressView = createAddressView();
}
public AddressResponse createAddressResponse() {
AddressDto addressDto = new AddressDto();
addressDto.setFirstName("tin");
addressDto.setLastName("tin");
addressDto.setCity("US");
addressDto.setZipCode("10212");
addressDto.setStreet1("street 1");
addressDto.setStreet2("street 2");
addressDto.setCountryCode("DE");
addressDto.setCompany("abc");
AddressResponse response = new AddressResponse();
response.setAddresses(Collections.singletonList(addressDto));
ValidationResult validationResult = new ValidationResult();
validationResult.setValidationStatus(JsonResponseStatus.OK);
response.setValidationResult(validationResult);
return response;
}
public AddressView createAddressView() {
AddressView addressView = new AddressView();
addressView.setFirstName("tin");
addressView.setLastName("tin");
addressView.setCity("US");
addressView.setZipCode("10212");
addressView.setStreet1("street 1");
addressView.setStreet2("street 2");
addressView.setCountryCode("DE");
addressView.setCompany("abc");
return addressView;
}
@Test
public void testCheckForCustomerAndUpdateAddress() throws UnexpectedException {
Mockito.when(customerService.updateAddress(addressView, UUID.randomUUID(), "BILLINGADDRESS", new JsonMessages())).thenReturn(addressResponse);
checkoutStepsAddressUtil.checkForCustomerAndUpdateAddress(UUID.randomUUID().toString(), checkoutAddressView, new JsonMessages(), UUID.randomUUID());
}
}
這是測試的實際方法
@Component
public class CheckoutStepsAddressUtil {
private static final Logger LOG = LoggerFactory.getLogger(CheckoutStepsAddressUtil.class);
@Autowired private CustomerService customerService;
@Autowired private UrlBuilder urlBuilder;
@Autowired private CustomerViewBuilder customerViewBuilder;
@Autowired private CheckoutViewBuilder checkoutViewBuilder;
@Autowired private CheckoutUtil checkoutUtil;
@Autowired private OfferService offerService;
public AddressView checkForCustomerAndUpdateAddress(String addressId, CheckoutAddressView checkoutView, JsonMessages messages, UUID customerId) throws UnexpectedException {
LOG.info("Entering");
AddressView addressView = null;
//check if the customer Id is null, if yes then return the error response else proceed to update
if (customerId == null) {
messages.addError(CheckoutStepAjaxControllerConstants.SHOP_CHECKOUT_ADDRESSES_MISSING_OFFER_OR_CUSTOMER);
LOG.info("Failed to store address because of missing customer");
} else {
//Trims the empty field values to null and proceed to update
checkoutUtil.trimEmptyAddressFieldsToNull(checkoutView);
addressView = updateAddressAndCheckAddressValidationResult(addressId, checkoutView, messages, customerId);
}
return addressView;
}
/**
* Calls Customer service to update the address and then checks the Validation Result with status`ERROR`
* and adds them to `JsonMessages`
*
* @param addressId id of the address to be updated
* @param checkoutView view that has the address to update
* @param messages
* @param customerId
* @return AddressView
* @throws UnexpectedException
*/
private AddressView updateAddressAndCheckAddressValidationResult(String addressId, CheckoutAddressView checkoutView, JsonMessages messages, UUID customerId) throws UnexpectedException {
AddressView address = checkoutView.getNewAddress();
address.setAddressId(addressId);
String identifier = OfferAddressType.NEW.toLower() + ADDRESS;
AddressResponse addressResponse = customerService.updateAddress(address, customerId, identifier, messages);
checkAddressValidationResponseFromCustomer(messages, identifier, addressResponse);
return address;
}
更新:通過這樣做解決了我的問題
@RunWith(MockitoJUnitRunner.class)
public class CheckoutStepsAddressUtilITest extends AbstractITest {
//Mock all the dependencies here
@Mock
private CustomerService customerService;
@Mock
private UrlBuilder urlBuilder;
@Mock
private CustomerViewBuilder customerViewBuilder;
@Mock
private CheckoutViewBuilder checkoutViewBuilder;
@Mock
private CheckoutUtil checkoutUtil;
@Mock
private OfferService offerService;
//Injects all the dependencies
@InjectMocks
private CheckoutStepsAddressUtil checkoutStepsAddressUtil;
//Test data
private AddressResponse addressResponse;
private CheckoutAddressView checkoutAddressView;
private AddressView actualAddressView;
@Before
public void setup() {
addressResponse = createAddressResponse();
checkoutAddressView = new CheckoutAddressView();
checkoutAddressView.setNewAddress(createAddressView());
actualAddressView = createAddressView();
}
@Test
public void testCheckForCustomerAndUpdateAddress() throws UnexpectedException {
Mockito.when(customerService.updateAddress(any(), any(), anyString(), any())).thenReturn(addressResponse);
AddressView expectedAddressView = checkoutStepsAddressUtil.checkForCustomerAndUpdateAddress(UUID.randomUUID().toString(), checkoutAddressView, new JsonMessages(), UUID.randomUUID());
assertNotNull(expectedAddressView);
assertEquals(actualAddressView.getFirstName(), expectedAddressView.getFirstName());
}
所謂的客戶服務不是您所嘲笑的。
測試中服務的自動裝配注釋將所有服務連接到CheckoutStepsAddressUtil中具有相同注釋的所有服務。 這意味着當您運行測試時,Spring無法得知應該用您的模擬代替customerService實例。 因此,呼叫實際服務。
您需要一種將模擬服務注入到要測試的服務中的方法。
一種方法是通過ReflectionTestUtils ,在實際調用被測方法之前,應在測試中添加以下代碼:
ReflectionTestUtils.setField(checkoutStepsAddressUtil, "customerService", customerService);
請注意,在這種情況下,您仍在自動裝配服務的其他依賴項,因此其他調用可能仍然有問題。
when.then中使用的某些對象與執行期間實際傳遞給此方法的對象不同。 我會在這里玩通配符:
@Test
public void testCheckForCustomerAndUpdateAddress() throws UnexpectedException {
UUID uuid = UUID.randomUUID();
Mockito.when(customerService.updateAddress(
eq(addressView), eq(uuid), eq("BILLINGADDRESS"), any(JsonMessages.class))
.thenReturn(addressResponse);
checkoutStepsAddressUtil.checkForCustomerAndUpdateAddress(uuid.toString(),checkoutAddressView, new JsonMessages(), uuid );
}
使用的: Mockito.any(), Mockito.eq()
;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.