How to use BDDMockito class of org.mockito package

Best Mockito code snippet using org.mockito.BDDMockito

Source:AccountPageControllerTest.java Github

copy

Full Screen

...64import org.apache.commons.collections.CollectionUtils;65import org.hamcrest.CoreMatchers;66import org.junit.Before;67import org.junit.Test;68import org.mockito.BDDMockito;69import org.mockito.InjectMocks;70import org.mockito.Mock;71import org.mockito.Mockito;72import org.mockito.MockitoAnnotations;73import org.springframework.context.MessageSource;74import org.springframework.ui.Model;75import org.springframework.validation.BindingResult;76import org.springframework.validation.support.BindingAwareModelMap;77import org.springframework.web.servlet.mvc.support.RedirectAttributes;78@UnitTest79public class AccountPageControllerTest80{81 private final Model page = Mockito.spy(new BindingAwareModelMap());82 private static final String VIEW_FOR_PAGE = "accountTest.jsp";83 private static final String VIEW_PREFIX = "pages/";84 private static final String FULL_VIEW_PATH = VIEW_PREFIX + VIEW_FOR_PAGE;85 private static final String TITLE_FOR_PAGE = "Account Test Title";86 public static final String CMS_PAGE_MODEL = "cmsPage";87 public static final String FIRST_NAME = "First";88 public static final String LAST_NAME = "Last";89 public static final String EMAIL = "hybris@hybris.com";90 public static final String TITLE_CODE = "Mr.";91 public static final String TEST_CODE = "12345";92 public static final String TEST_COUNTRY_CODE = "US";93 private static final String REDIRECT_TO_EDIT_ADDRESS_PAGE = "redirect:/my-account/edit-address/";94 private static final String REDIRECT_TO_UPDATE_PROFILE = "redirect:/my-account/update-profile";95 private static final String REDIRECT_TO_PAYMENT_INFO_PAGE = "redirect:/my-account/payment-details";96 private static final String REDIRECT_TO_PASSWORD_UPDATE_PAGE = "redirect:/my-account/update-password";97 private static final String REDIRECT_TO_ADDRESS_BOOK_PAGE = "redirect:/my-account/address-book";98 private static final String REDIRECT_TO_ORDER_HISTORY_PAGE = "redirect:/my-account/orders";99 private static final String UPDATE_EMAIL_CMS_PAGE = "update-email";100 private static final String UPDATE_PROFILE_CMS_PAGE = "update-profile";101 @InjectMocks102 private final AccountPageController accountController = Mockito.spy(new AccountPageController());103 @Mock104 private UserFacade userFacade;105 @Mock106 private OrderFacade orderFacade;107 @Mock108 private CheckoutFacade checkoutFacade;109 @Mock110 private CustomerFacade customerFacade;111 @Mock112 private AddressVerificationFacade addressVerificationFacade;113 @Mock114 private I18NFacade i18NFacade;115 @Mock116 private I18NService i18NService;117 @Mock118 private ContentPageModel contentPageModel;119 @Mock120 private AddressData addressData;121 @Mock122 private CountryData countryData;123 @Mock124 private RegionData regionData;125 @Mock126 private CustomerData customerData;127 @Mock128 private TitleData titleData;129 @Mock130 private PaginationData paginationData;131 @Mock132 private OrderHistoryData orderHistoryData;133 @Mock134 private OrderData orderData;135 @Mock136 private ResourceBreadcrumbBuilder accountBreadcrumbBuilder;137 @Mock138 private Breadcrumb breadcrumb;139 @Mock140 private MessageSource messageSource;141 @Mock142 private CMSPageService cmsPageService;143 @Mock144 private PageTitleResolver pageTitleResolver;145 @Mock146 private PageTemplateModel pageTemplateModel;147 @Mock148 private AbstractPageModel abstractPageModel;149 @Mock150 private AddressForm addressForm;151 @Mock152 private UpdateEmailForm emailForm;153 @Mock154 private UpdateProfileForm profileForm;155 @Mock156 private UpdatePasswordForm passwordForm;157 @Mock158 private BindingResult bindingResult;159 @Mock160 private RedirectAttributes redirectModel;161 @Mock162 private AddressVerificationResultHandler addressVerificationResultHandler;163 @Mock164 private PasswordValidator passwordValidator; //NOPMD165 @Mock166 private EmailValidator emailValidator; //NOPMD167 @Mock168 private AddressValidator addressValidator; //NOPMD169 @Mock170 private ProfileValidator profileValidator; //NOPMD171 @Mock172 private SiteConfigService siteConfigService; //NOPMD173 private List breadcrumbsList;174 private SearchPageData<OrderHistoryData> searchList;175 @InjectMocks176 private final AddressDataUtil addressConverter = Mockito.spy(new AddressDataUtil());177 @Before178 public void prepare() throws CMSItemNotFoundException179 {180 MockitoAnnotations.initMocks(this);181 final Locale locale = new Locale("en");182 final List breadcrumbsList = new ArrayList();183 breadcrumbsList.add(breadcrumb);184 BDDMockito.given(accountBreadcrumbBuilder.getBreadcrumbs(Mockito.anyString())).willReturn(breadcrumbsList);185 BDDMockito.given(cmsPageService.getPageForLabelOrId(Mockito.anyString())).willReturn(contentPageModel);186 BDDMockito.given(pageTitleResolver.resolveContentPageTitle(Mockito.anyString())).willReturn(TITLE_FOR_PAGE);187 BDDMockito.given(Boolean.valueOf(page.containsAttribute(CMS_PAGE_MODEL))).willReturn(Boolean.TRUE);188 BDDMockito.given(page.asMap().get(CMS_PAGE_MODEL)).willReturn(abstractPageModel);189 BDDMockito.given(abstractPageModel.getMasterTemplate()).willReturn(pageTemplateModel);190 BDDMockito.given(cmsPageService.getFrontendTemplateName(pageTemplateModel)).willReturn(VIEW_FOR_PAGE);191 BDDMockito.given(checkoutFacade.getDeliveryCountries()).willReturn(Collections.singletonList(countryData));192 BDDMockito.given(userFacade.getTitles()).willReturn(Collections.singletonList(titleData));193 BDDMockito.given(customerData.getFirstName()).willReturn(FIRST_NAME);194 BDDMockito.given(customerData.getLastName()).willReturn(LAST_NAME);195 BDDMockito.given(customerData.getTitleCode()).willReturn(TITLE_CODE);196 BDDMockito.given(customerData.getUid()).willReturn(FIRST_NAME);197 BDDMockito.given(customerFacade.getCurrentCustomer()).willReturn(customerData);198 BDDMockito.given(i18NService.getCurrentLocale()).willReturn(locale);199 BDDMockito.given(i18NFacade.getRegionsForCountryIso(Mockito.anyString())).willReturn(Collections.singletonList(regionData));200 BDDMockito.given(messageSource.getMessage(Mockito.anyString(), Mockito.any(Object[].class), Mockito.eq(locale)))201 .willReturn("ANY STRING");202 BDDMockito.given(i18NFacade.getCountryForIsocode(Mockito.anyString())).willReturn(countryData);203 }204 private void setupAddressCreateEdit()205 {206 BDDMockito.doReturn(addressForm).when(accountController).getPreparedAddressForm();207 }208 private void createBasicAddressFields()209 {210 BDDMockito.given(addressForm.getFirstName()).willReturn(FIRST_NAME);211 BDDMockito.given(addressForm.getLastName()).willReturn(LAST_NAME);212 BDDMockito.given(addressForm.getLine1()).willReturn("LINE 1");213 BDDMockito.given(addressForm.getCountryIso()).willReturn("US");214 BDDMockito.given(addressForm.getRegionIso()).willReturn("NY");215 BDDMockito.given(addressForm.getPostcode()).willReturn("12345");216 }217 private void createEmailForm(final String email, final String password)218 {219 BDDMockito.given(emailForm.getEmail()).willReturn(email);220 BDDMockito.given(emailForm.getPassword()).willReturn(password);221 }222 private void setupExistingOrder()223 {224 BDDMockito.given(Integer.valueOf(paginationData.getNumberOfPages())).willReturn(Integer.valueOf(1));225 final ArrayList orderHistory = new ArrayList<OrderHistoryData>();226 orderHistory.add(orderHistoryData);227 searchList = new SearchPageData<OrderHistoryData>();228 searchList.setResults(orderHistory);229 searchList.setPagination(paginationData);230 }231 // Address Tests232 @Test233 public void shouldGetAddressForm()234 {235 BDDMockito.given(accountController.getCountries()).willReturn(Collections.singletonList(countryData));236 final String countryFragment = accountController.getCountryAddressForm("TEST_ADDRESS_CODE", TEST_COUNTRY_CODE, page);237 Mockito.verify(page).addAttribute("supportedCountries", accountController.getCountries());238 Mockito.verify(page).addAttribute("regions", i18NFacade.getRegionsForCountryIso(TEST_COUNTRY_CODE));239 Mockito.verify(page).addAttribute("country", TEST_COUNTRY_CODE);240 assertEquals(ControllerConstants.Views.Fragments.Account.CountryAddressForm, countryFragment);241 }242 @Test243 public void shouldGetAddressBook() throws CMSItemNotFoundException244 {245 BDDMockito.given(userFacade.getAddressBook()).willReturn(Collections.singletonList(addressData));246 final String addressBookPage = accountController.getAddressBook(page);247 Mockito.verify(page).addAttribute("addressData", Collections.singletonList(addressData));248 Mockito.verify(page).addAttribute("cmsPage", contentPageModel);249 Mockito.verify(page).addAttribute("pageTitle", TITLE_FOR_PAGE);250 assertEquals(FULL_VIEW_PATH, addressBookPage);251 }252 @Test253 public void shouldPrepareAddress()254 {255 final AddressForm addressForm = accountController.getPreparedAddressForm();256 assertEquals(FIRST_NAME, addressForm.getFirstName());257 assertEquals(LAST_NAME, addressForm.getLastName());258 assertEquals(TITLE_CODE, addressForm.getTitleCode());259 }260 @Test261 public void shouldGetAddAddress() throws CMSItemNotFoundException262 {263 BDDMockito.given(userFacade.getAddressBook()).willReturn(Collections.singletonList(addressData));264 setupAddressCreateEdit();265 final String addAddressPage = accountController.addAddress(page);266 Mockito.verify(page).addAttribute("countryData", Collections.singletonList(countryData));267 Mockito.verify(page).addAttribute("titleData", Collections.singletonList(titleData));268 Mockito.verify(page).addAttribute("addressForm", addressForm);269 Mockito.verify(page).addAttribute("addressBookEmpty", Boolean.FALSE);270 Mockito.verify(page).addAttribute("isDefaultAddress", Boolean.FALSE);271 assertEquals(FULL_VIEW_PATH, addAddressPage);272 }273 @Test274 public void shouldNotCreateInvalidAddress() throws CMSItemNotFoundException275 {276 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);277 final String addAddressPage = accountController.addAddress(addressForm, bindingResult, page, redirectModel);278 Mockito.verify(accountController).setUpAddressFormAfterError(addressForm, page);279 assertEquals(FULL_VIEW_PATH, addAddressPage);280 }281 @Test282 public void shouldSuggestValidAddress() throws CMSItemNotFoundException283 {284 final AddressVerificationResult<AddressVerificationDecision> avsResult = new AddressVerificationResult<AddressVerificationDecision>();285 avsResult.setDecision(AddressVerificationDecision.REVIEW);286 createBasicAddressFields();287 BDDMockito.given(addressVerificationFacade.verifyAddressData(Mockito.any(AddressData.class))).willReturn(avsResult);288 BDDMockito.given(Boolean.valueOf(289 addressVerificationResultHandler.handleResult(Mockito.eq(avsResult), Mockito.any(AddressData.class), Mockito.eq(page),290 Mockito.eq(redirectModel), Mockito.eq(bindingResult), Mockito.anyBoolean(), Mockito.anyString())))291 .willReturn(Boolean.TRUE);292 final String addAddressPage = accountController.addAddress(addressForm, bindingResult, page, redirectModel);293 assertEquals(FULL_VIEW_PATH, addAddressPage);294 }295 @Test296 public void shouldCreateValidAddress() throws CMSItemNotFoundException297 {298 final AddressVerificationResult<AddressVerificationDecision> avsResult = new AddressVerificationResult<AddressVerificationDecision>();299 avsResult.setDecision(AddressVerificationDecision.ACCEPT);300 createBasicAddressFields();301 BDDMockito.given(addressVerificationFacade.verifyAddressData(Mockito.any(AddressData.class))).willReturn(avsResult);302 BDDMockito.given(Boolean.valueOf(303 addressVerificationResultHandler.handleResult(Mockito.eq(avsResult), Mockito.any(AddressData.class), Mockito.eq(page),304 Mockito.eq(redirectModel), Mockito.eq(bindingResult), Mockito.anyBoolean(), Mockito.anyString())))305 .willReturn(Boolean.FALSE);306 final String addAddressPage = accountController.addAddress(addressForm, bindingResult, page, redirectModel);307 Mockito.verify(userFacade).addAddress(Mockito.any(AddressData.class));308 assertThat(addAddressPage, CoreMatchers.containsString(REDIRECT_TO_EDIT_ADDRESS_PAGE));309 }310 @Test311 public void shouldGetEditAddress() throws CMSItemNotFoundException312 {313 final String addressBookPage = accountController.editAddress(TEST_CODE, page);314 Mockito.verify(page).addAttribute("countryData", checkoutFacade.getDeliveryCountries());315 Mockito.verify(page).addAttribute("titleData", userFacade.getTitles());316 Mockito.verify(page).addAttribute("addressBookEmpty",317 Boolean.valueOf(CollectionUtils.isEmpty(userFacade.getAddressBook())));318 Mockito.verify(page).addAttribute(ThirdPartyConstants.SeoRobots.META_ROBOTS,319 ThirdPartyConstants.SeoRobots.NOINDEX_NOFOLLOW);320 Mockito.verify(page).addAttribute("edit", Boolean.TRUE);321 assertEquals(FULL_VIEW_PATH, addressBookPage);322 }323 @Test324 public void shouldNotUpdateInvalidAddress() throws CMSItemNotFoundException325 {326 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);327 final String addressBookPage = accountController.editAddress(addressForm, bindingResult, page, redirectModel);328 Mockito.verify(accountController).setUpAddressFormAfterError(addressForm, page);329 assertEquals(FULL_VIEW_PATH, addressBookPage);330 }331 @Test332 public void shouldSuggestValidUpdateAddress() throws CMSItemNotFoundException333 {334 final AddressVerificationResult<AddressVerificationDecision> avsResult = new AddressVerificationResult<AddressVerificationDecision>();335 avsResult.setDecision(AddressVerificationDecision.REVIEW);336 createBasicAddressFields();337 BDDMockito.given(addressVerificationFacade.verifyAddressData(Mockito.any(AddressData.class))).willReturn(avsResult);338 BDDMockito.given(Boolean.valueOf(339 addressVerificationResultHandler.handleResult(Mockito.eq(avsResult), Mockito.any(AddressData.class), Mockito.eq(page),340 Mockito.eq(redirectModel), Mockito.eq(bindingResult), Mockito.anyBoolean(), Mockito.anyString())))341 .willReturn(Boolean.TRUE);342 final String addAddressPage = accountController.editAddress(addressForm, bindingResult, page, redirectModel);343 assertEquals(FULL_VIEW_PATH, addAddressPage);344 }345 @Test346 public void shouldUpdateValidAddress() throws CMSItemNotFoundException347 {348 final String editAddressPage = accountController.editAddress(addressForm, bindingResult, page, redirectModel);349 Mockito.verify(userFacade).editAddress(Mockito.any(AddressData.class));350 assertThat(editAddressPage, CoreMatchers.containsString(REDIRECT_TO_EDIT_ADDRESS_PAGE));351 }352 @Test353 public void shouldSetDefaultAddress()354 {355 final String addressBookPage = accountController.setDefaultAddress(TEST_CODE, redirectModel);356 Mockito.verify(userFacade).setDefaultAddress(Mockito.any(AddressData.class));357 assertEquals(REDIRECT_TO_ADDRESS_BOOK_PAGE, addressBookPage);358 }359 @Test360 public void shouldRemoveAddress()361 {362 final String addressBookPage = accountController.removeAddress(TEST_CODE, redirectModel);363 Mockito.verify(userFacade).removeAddress(Mockito.any(AddressData.class));364 assertEquals(REDIRECT_TO_ADDRESS_BOOK_PAGE, addressBookPage);365 }366 // Orders Tests367 @Test368 public void shouldGetAllOrders() throws CMSItemNotFoundException369 {370 final ShowMode showMode = ShowMode.All;371 setupExistingOrder();372 BDDMockito.given(orderFacade.getPagedOrderHistoryForStatuses(Mockito.any(PageableData.class))).willReturn(searchList);373 final String orderHistoryPage = accountController.orders(1, showMode, "desc", page);374 Mockito.verify(orderFacade).getPagedOrderHistoryForStatuses(Mockito.any(PageableData.class));375 assertEquals(FULL_VIEW_PATH, orderHistoryPage);376 }377 @Test378 public void shouldNotGetNonExistingOrder() throws CMSItemNotFoundException379 {380 BDDMockito.given(orderFacade.getOrderDetailsForCode(TEST_CODE)).willThrow(UnknownIdentifierException.class);381 final String orderHistoryPage = accountController.order(TEST_CODE, page, redirectModel);382 BDDMockito.verify(page, BDDMockito.times(0)).addAttribute(Mockito.anyString(), Mockito.anyString());383 assertEquals(REDIRECT_TO_ORDER_HISTORY_PAGE, orderHistoryPage);384 }385 @Test386 public void shouldGetExistingOrder() throws CMSItemNotFoundException387 {388 setupExistingOrder();389 BDDMockito.given(orderFacade.getOrderDetailsForCode(TEST_CODE)).willReturn(orderData);390 final String myAccountPage = accountController.order(TEST_CODE, page, redirectModel);391 BDDMockito.verify(page, BDDMockito.times(6)).addAttribute(Mockito.anyString(), Mockito.anyString());392 assertEquals(FULL_VIEW_PATH, myAccountPage);393 }394 // Profile Tests395 @Test396 public void shouldGetProfile() throws CMSItemNotFoundException397 {398 final String updateProfilePage = accountController.profile(page);399 BDDMockito.verify(page, BDDMockito.times(7)).addAttribute(Mockito.anyString(), Mockito.anyString());400 assertEquals(FULL_VIEW_PATH, updateProfilePage);401 }402 @Test403 public void shouldGetUpdateProfile() throws CMSItemNotFoundException404 {405 final String profilePage = accountController.editProfile(page);406 BDDMockito.verify(page, BDDMockito.times(7)).addAttribute(Mockito.anyString(), Mockito.anyString());407 assertEquals(FULL_VIEW_PATH, profilePage);408 }409 @Test410 public void shouldNotUpdateProfile() throws CMSItemNotFoundException, DuplicateUidException411 {412 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);413 final String profilePage = accountController.updateProfile(profileForm, bindingResult, page, redirectModel);414 BDDMockito.verify(customerFacade, BDDMockito.times(0)).updateProfile(Mockito.any(CustomerData.class));415 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_PROFILE_CMS_PAGE);416 assertEquals(FULL_VIEW_PATH, profilePage);417 }418 @Test419 public void shouldNotUpdateDuplicateUidProfile() throws CMSItemNotFoundException, DuplicateUidException420 {421 BDDMockito.doThrow(new DuplicateUidException()).when(customerFacade).updateProfile(Mockito.any(CustomerData.class));422 final String profilePage = accountController.updateProfile(profileForm, bindingResult, page, redirectModel);423 BDDMockito.verify(customerFacade).updateProfile(Mockito.any(CustomerData.class));424 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_PROFILE_CMS_PAGE);425 assertEquals(FULL_VIEW_PATH, profilePage);426 }427 @Test428 public void shouldUpdateProfile() throws CMSItemNotFoundException, DuplicateUidException429 {430 final String profilePage = accountController.updateProfile(profileForm, bindingResult, page, redirectModel);431 BDDMockito.verify(customerFacade).updateProfile(Mockito.any(CustomerData.class));432 assertEquals(REDIRECT_TO_UPDATE_PROFILE, profilePage);433 }434 // E-mail Tests435 @Test436 public void shouldGetEmail() throws CMSItemNotFoundException437 {438 final String emailPage = accountController.editEmail(page);439 BDDMockito.verify(page, BDDMockito.times(6)).addAttribute(Mockito.anyString(), Mockito.anyString());440 assertEquals(FULL_VIEW_PATH, emailPage);441 }442 @Test443 public void shouldNotUpdateInavlidEmail() throws CMSItemNotFoundException444 {445 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);446 final String emailUpdatePage = accountController.updateEmail(emailForm, bindingResult, page, redirectModel);447 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_EMAIL_CMS_PAGE);448 assertEquals(FULL_VIEW_PATH, emailUpdatePage);449 }450 @Test451 public void shouldNotUpdateDuplicateEmail() throws CMSItemNotFoundException, PasswordMismatchException, DuplicateUidException452 {453 createEmailForm(EMAIL, FIRST_NAME);454 BDDMockito.doThrow(new DuplicateUidException()).when(customerFacade).changeUid(Mockito.anyString(), Mockito.anyString());455 final String emailUpdatePage = accountController.updateEmail(emailForm, bindingResult, page, redirectModel);456 BDDMockito.verify(bindingResult).rejectValue("email", "profile.email.unique");457 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_EMAIL_CMS_PAGE);458 assertEquals(FULL_VIEW_PATH, emailUpdatePage);459 }460 @Test461 public void shouldNotUpdateInvalidPassword() throws CMSItemNotFoundException, PasswordMismatchException, DuplicateUidException462 {463 createEmailForm(EMAIL, "123");464 BDDMockito.doThrow(new PasswordMismatchException("error")).when(customerFacade).changeUid(Mockito.anyString(),465 Mockito.anyString());466 final String emailUpdatePage = accountController.updateEmail(emailForm, bindingResult, page, redirectModel);467 BDDMockito.verify(bindingResult).rejectValue("password", "profile.currentPassword.invalid");468 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_EMAIL_CMS_PAGE);469 assertEquals(FULL_VIEW_PATH, emailUpdatePage);470 }471 // Password Tests472 @Test473 public void shouldGetPassword() throws CMSItemNotFoundException474 {475 final String passwordPage = accountController.updatePassword(page);476 BDDMockito.verify(page).addAttribute(BDDMockito.eq("updatePasswordForm"), Mockito.any(UpdatePasswordForm.class));477 assertEquals(FULL_VIEW_PATH, passwordPage);478 }479 @Test480 public void shouldNotUpdatePassword() throws CMSItemNotFoundException481 {482 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);483 BDDMockito.given(accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile.updatePasswordForm"))484 .willReturn(breadcrumbsList);485 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);486 BDDMockito.verify(page).addAttribute("breadcrumbs", breadcrumbsList);487 assertEquals(FULL_VIEW_PATH, passwordPage);488 }489 @Test490 public void shouldNotUpdatePasswordNotEqual() throws CMSItemNotFoundException491 {492 BDDMockito.given(passwordForm.getCurrentPassword()).willReturn(TEST_CODE);493 BDDMockito.given(passwordForm.getNewPassword()).willReturn("Different");494 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);495 BDDMockito.verify(bindingResult).rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},496 "validation.checkPwd.equals");497 assertEquals(REDIRECT_TO_PASSWORD_UPDATE_PAGE, passwordPage);498 }499 @Test500 public void shouldNotUpdatePasswordNotValid() throws CMSItemNotFoundException501 {502 BDDMockito.given(passwordForm.getCheckNewPassword()).willReturn(TEST_CODE);503 BDDMockito.given(passwordForm.getNewPassword()).willReturn(TEST_CODE);504 Mockito.doThrow(new PasswordMismatchException("error")).when(customerFacade).changePassword(Mockito.anyString(),505 Mockito.anyString());506 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);507 BDDMockito.verify(bindingResult).rejectValue("currentPassword", "profile.currentPassword.invalid", new Object[] {},508 "profile.currentPassword.invalid");509 assertEquals(REDIRECT_TO_PASSWORD_UPDATE_PAGE, passwordPage);510 }511 @Test512 public void shouldUpdatePassword() throws CMSItemNotFoundException513 {514 BDDMockito.given(passwordForm.getCheckNewPassword()).willReturn(TEST_CODE);515 BDDMockito.given(passwordForm.getNewPassword()).willReturn(TEST_CODE);516 BDDMockito.given(passwordForm.getCurrentPassword()).willReturn(TEST_CODE);517 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);518 BDDMockito.verify(customerFacade).changePassword(TEST_CODE, TEST_CODE);519 assertEquals(REDIRECT_TO_PASSWORD_UPDATE_PAGE, passwordPage);520 }521 // Payment Tests522 @Test523 public void shouldGetPaymentDetails() throws CMSItemNotFoundException524 {525 final String paymentDetailsPage = accountController.paymentDetails(page);526 BDDMockito.verify(page, BDDMockito.times(7)).addAttribute(Mockito.anyString(), Mockito.anyString());527 assertEquals(FULL_VIEW_PATH, paymentDetailsPage);528 }529 @Test530 public void shouldSetDefaultPaymentDetailsNull() throws CMSItemNotFoundException531 {532 final String paymentDetailsPage = accountController.setDefaultPaymentDetails(null);533 BDDMockito.verify(userFacade, BDDMockito.times(0)).getCCPaymentInfoForCode(Mockito.anyString());534 BDDMockito.verify(userFacade).setDefaultPaymentInfo(Mockito.any(CCPaymentInfoData.class));535 assertEquals(REDIRECT_TO_PAYMENT_INFO_PAGE, paymentDetailsPage);536 }537 @Test538 public void shouldSetDefaultPaymentDetails() throws CMSItemNotFoundException539 {540 final String paymentDetailsPage = accountController.setDefaultPaymentDetails(TEST_CODE);541 BDDMockito.verify(userFacade, BDDMockito.times(1)).getCCPaymentInfoForCode(Mockito.anyString());542 BDDMockito.verify(userFacade).setDefaultPaymentInfo(Mockito.any(CCPaymentInfoData.class));543 assertEquals(REDIRECT_TO_PAYMENT_INFO_PAGE, paymentDetailsPage);544 }545 @Test546 public void shouldRemovePaymentMethod() throws CMSItemNotFoundException547 {548 final String paymentDetailsPage = accountController.removePaymentMethod(TEST_CODE, redirectModel);549 BDDMockito.verify(userFacade, BDDMockito.times(1)).unlinkCCPaymentInfo(Mockito.anyString());550 assertEquals(REDIRECT_TO_PAYMENT_INFO_PAGE, paymentDetailsPage);551 }552}...

Full Screen

Full Screen

BDDMockito

Using AI Code Generation

copy

Full Screen

1import static org.mockito.BDDMockito.given;2import static org.mockito.BDDMockito.then;3import static org.mockito.Mockito.mock;4import static org.mockito.Mockito.verify;5import static org.mockito.Mockito.verifyNoMoreInteractions;6import static org.mockito.Mockito.when;7import java.util.ArrayList;8import java.util.Iterator;9import java.util.List;10import org.junit.Test;11import org.mockito.InOrder;12import org.mockito.Mockito;13public class MockitoTest {14 public void whenNotUseMockAnnotation_thenCorrect() {15 MockList mockList = mock(MockList.class);16 when(mockList.size()).thenReturn(10);17 mockList.add("one");18 mockList.add("two");19 mockList.add("three");20 verify(mockList).add("one");21 verify(mockList).add("two");22 verify(mockList).add("three");23 verify(mockList).size();24 verifyNoMoreInteractions(mockList);25 }26 public void whenNotUseBDDAnnotation_thenCorrect() {27 MockList mockList = mock(MockList.class);28 given(mockList.size()).willReturn(10);29 mockList.add("one");30 mockList.add("two");31 mockList.add("three");32 then(mockList).should().add("one");33 then(mockList).should().add("two");34 then(mockList).should().add("three");35 then(mockList).should().size();36 then(mockList).shouldHaveNoMoreInteractions();37 }38 public void whenUseInOrderAnnotation_thenCorrect() {39 List<String> mockList = mock(List.class);40 mockList.add("one");41 mockList.add("two");42 mockList.add("three");43 InOrder inOrder = Mockito.inOrder(mockList);44 inOrder.verify(mockList).add("one");45 inOrder.verify(mockList).add("two");46 inOrder.verify(mockList).add("three");47 }48 public void whenUseSpyAnnotation_thenCorrect() {49 List<String> spyList = Mockito.spy(new ArrayList<String>());50 spyList.add("one");51 spyList.add("two");52 verify(spyList).add("one");

Full Screen

Full Screen

BDDMockito

Using AI Code Generation

copy

Full Screen

1import static org.mockito.BDDMockito.given;2import static org.mockito.BDDMockito.then;3import static org.mockito.BDDMockito.willReturn;4import static org.mockito.Mockito.mock;5import static org.mockito.Mockito.verify;6import static org.mockito.Mockito.when;7import org.junit.runner.RunWith;8import org.mockito.runners.MockitoJUnitRunner;9@RunWith(MockitoJUnitRunner.class)10public class MockitoTest {11 private List<String> mockedList;12 public void testMockito() {13 mockedList.add("one");14 mockedList.clear();15 verify(mockedList).add("one");16 verify(mockedList).clear();17 }18 public void testMockitoStubbing() {19 LinkedList mockedList = mock(LinkedList.class);20 when(mockedList.get(0)).thenReturn("first");21 when(mockedList.get(1)).thenThrow(new RuntimeException());22 System.out.println(mockedList.get(0));23 System.out.println(mockedList.get(1));24 System.out.println(mockedList.get(999));25 verify(mockedList).get(0);26 }27 public void testMockitoBDDStyle() {28 given(mockedList.get(0)).willReturn("first");29 System.out.println(mockedList.get(0));30 then(mockedList).should().get(0);31 }32 public void testMockitoBDDStyle2() {33 willReturn("first").given(mockedList).get(0);

Full Screen

Full Screen

BDDMockito

Using AI Code Generation

copy

Full Screen

1import org.mockito.BDDMockito.*;2import org.mockito.Mockito.*;3import org.mockito.runners.MockitoJUnitRunner;4import org.mockito.junit.MockitoRule;5import org.mockito.junit.MockitoJUnit;6import org.mockito.MockitoAnnotations;7import org.mockito.MockitoSession;8import org.mockito.MockitoSessionBuilder;9import org.mockito.MockitoSessionLogger;10import org.mockito.MockitoSessionLoggerBase;11import org.mockito.MockitoSessionLoggerFactory;12import org.mockito.MockitoSessionLoggerProvider;13import org.mockito.MockitoSessionLoggerProviderBase;14import org.mockito.MockitoSessionLoggerProviderFactory;15import org.mockito.MockitoSessionLoggerProviderImpl;16import org.mockito.MockitoSessionLoggerProviderImplBase;17import org.mockito.MockitoSessionLoggerProviderImplFactory;18import org.mockito.MockitoSessionLoggerProviderImplProvider;19import org.mockito.MockitoSessionLoggerProviderImplProviderBase;20import org.mockito.MockitoSessionLoggerProviderImplProviderFactory;21import org.mockito.MockitoSessionLoggerProviderImplProviderImpl;22import org.mockito.MockitoSessionLoggerProviderImplProviderImplBase;

Full Screen

Full Screen

BDDMockito

Using AI Code Generation

copy

Full Screen

1import static org.mockito.BDDMockito.given;2import static org.mockito.BDDMockito.then;3import static org.mockito.Matchers.any;4import static org.mockito.Mockito.mock;5import static org.mockito.Mockito.when;6import java.util.ArrayList;7import java.util.List;8import org.junit.Test;9import org.mockito.ArgumentCaptor;10import org.mockito.InOrder;11import org.mockito.invocation.InvocationOnMock;12import org.mockito.stubbing.Answer;13public class MockitoTest {14 public void test() {15 List mockedList = mock(List.class);16 mockedList.add("one");17 mockedList.clear();18 when(mockedList.get(0)).thenReturn("first");19 when(mockedList.get(1)).thenThrow(new RuntimeException());20 System.out.println(mockedList.get(0));21 System.out.println(mockedList.get(999));22 when(mockedList.get(anyInt())).thenReturn("element");23 verify(mockedList).get(anyInt());24 when(mockedList.contains(argThat(isValid()))).thenReturn(true);25 verify(mockedList).contains(argThat(isValid()));26 when(mockedList.contains

Full Screen

Full Screen

BDDMockito

Using AI Code Generation

copy

Full Screen

1import static org.mockito.BDDMockito.given;2import static org.mockito.BDDMockito.then;3import static org.mockito.Mockito.times;4import static org.mockito.Mockito.verify;5import static org.mockito.Mockito.when;6import static org.hamcrest.Matchers.*;7import static org.hamcrest.MatcherAssert.assertThat;8import org.junit.jupiter.api.Test;9import org.mockito.ArgumentCaptor;10import org.mockito.BDDMockito;11import org.mockito.Mockito;12import org.mockito.invocation.InvocationOnMock;13import org.mockito.stubbing.Answer;14import java.util.ArrayList;15import java.util.List;16class ListTest {17 List<String> mock = Mockito.mock(List.class);18 void size_basic() {19 when(mock.size()).thenReturn(5);20 assertThat(mock.size(), is(5));21 }22 void returnDifferentValues() {23 when(mock.size()).thenReturn(5).thenReturn(10);24 assertThat(mock.size(), is(5));25 assertThat(mock.size(), is(10));26 }27 void returnWithParameters() {28 when(mock.get(0)).thenReturn("in28Minutes");29 assertThat(mock.get(0), is("in28Minutes"));30 assertThat(mock.get(1), is(nullValue()));31 }32 void returnWithGenericParameters() {33 when(mock.get(Mockito.anyInt())).thenReturn("in28Minutes");34 assertThat(mock.get(0), is("in28Minutes"));35 assertThat(mock.get(1), is("in28Minutes"));36 }37 void verificationBasics() {38 String value1 = mock.get(0);39 String value2 = mock.get(1);40 verify(mock).get(0);41 verify(mock, times(2)).get(Mockito.anyInt());42 verify(mock, Mockito.never()).get(2);43 }44 void argumentCapturing() {45 mock.add("SomeString");46 ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);47 verify(mock).add(captor.capture());48 assertThat(captor.getValue(), is("SomeString"));49 }50 void multipleArgumentCapturing() {51 mock.add("SomeString1");52 mock.add("SomeString2");

Full Screen

Full Screen

BDDMockito

Using AI Code Generation

copy

Full Screen

1import static org.mockito.BDDMockito.given;2import static org.mockito.Mockito.mock;3import static org.mockito.Mockito.verify;4import static org.mockito.Mockito.when;5import static org.mockito.MockitoAnnotations.initMocks;6import static org.testng.Assert.assertEquals;7import static org.testng.Assert.assertNotNull;8import static org.testng.Assert.assertTrue;9import static org.testng.Assert.fail;10import java.util.List;11import org.mockito.InjectMocks;12import org.mockito.Mock;13import org.mockito.Mockito;14import org.mockito.Spy;15import org.mockito.invocation.InvocationOnMock;16import org.mockito.stubbing.Answer;17import org.testng.annotations.BeforeMethod;18import org.testng.annotations.Test;19import com.google.common.collect.Lists;20public class BDDMockitoTest {21 private List<String> mockedList;22 private List<String> spiedList = Lists.newArrayList();23 private List<String> injectedList = Lists.newArrayList();24 public void setUp() {25 initMocks(this);26 }27 public void testMock() {28 assertNotNull(mockedList);29 mockedList.add("one");30 Mockito.verify(mockedList).add("one");31 }32 public void testSpy() {33 assertNotNull(spiedList);34 spiedList.add("one");35 Mockito.verify(spiedList).add("one");36 }37 public void testInjectMocks() {38 assertNotNull(injectedList);39 injectedList.add("one");40 Mockito.verify(injectedList).add("one");41 }42 public void testGivenWhenThen() {43 given(mockedList.get(0)).willReturn("first");44 String firstElement = mockedList.get(0);45 assertEquals(firstElement, "first");46 }47 public void testVerify() {48 given(mockedList.get(0)).willReturn("first");49 String firstElement = mockedList.get(0);50 verify(mockedList).get(0);51 }52 public void testWillThrow() {53 given(mockedList.get(0)).willThrow(new RuntimeException("Test Exception"));54 try {55 mockedList.get(0);56 fail("should throw exception");57 } catch (Exception e) {

Full Screen

Full Screen

BDDMockito

Using AI Code Generation

copy

Full Screen

1import static org.mockito.BDDMockito.given;2import static org.mockito.Mockito.mock;3import java.util.List;4import org.junit.Test;5public class BDDMockitoTest {6 public void whenUseBDDMockito_thenCorrect() {7 List<String> mockList = mock(List.class);8 given(mockList.get(0)).willReturn("in28Minutes");9 String firstElement = mockList.get(0);10 assertEquals("in28Minutes", firstElement);11 }12}13package com.in28minutes.junit.helper;14import static org.junit.Assert.*;15import java.util.Arrays;16import java.util.List;17import org.junit.Test;18public class ListTest {19 public void test() {20 List<Integer> numbers = Arrays.asList(12, 15, 45);21 assertEquals(3, numbers.size());22 }23}24package com.in28minutes.junit.helper;25import static org.junit.Assert.*;26import java.util.Arrays;27import java.util.List;28import org.junit.Test;29public class ListTest {30 public void test() {31 List<Integer> numbers = Arrays.asList(12, 15, 45);32 assertEquals(3, numbers.size());33 }34}35package com.in28minutes.junit.helper;36import static org.junit.Assert.*;37import java.util.Arrays;38import java.util.List;39import org.junit.Test;40public class ListTest {41 public void test() {42 List<Integer> numbers = Arrays.asList(12, 15, 45);43 assertEquals(3, numbers.size());44 }45}46package com.in28minutes.junit.helper;47import static org.junit.Assert.*;48import java.util.Arrays;49import java.util.List;50import org.junit.Test;51public class ListTest {52 public void test() {53 List<Integer> numbers = Arrays.asList(12, 15, 45);54 assertEquals(3, numbers.size());55 }56}57package com.in28minutes.junit.helper;58import static org.junit.Assert.*;59import java.util.Arrays;60import java.util.List;61import org.junit.Test;62public class ListTest {63 public void test() {64 List<Integer> numbers = Arrays.asList(12, 15, 45);65 assertEquals(3, numbers.size());66 }67}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful