Best Mockito code snippet using org.mockito.BDDMockito.should
Source:AccountPageControllerTest.java
...230 searchList.setPagination(paginationData);231 }232 // Address Tests233 @Test234 public void shouldGetAddressForm()235 {236 BDDMockito.given(accountController.getCountries()).willReturn(Collections.singletonList(countryData));237 final String countryFragment = accountController.getCountryAddressForm("TEST_ADDRESS_CODE", TEST_COUNTRY_CODE, page);238 Mockito.verify(page).addAttribute("supportedCountries", accountController.getCountries());239 Mockito.verify(page).addAttribute("regions", i18NFacade.getRegionsForCountryIso(TEST_COUNTRY_CODE));240 Mockito.verify(page).addAttribute("country", TEST_COUNTRY_CODE);241 assertEquals(ControllerConstants.Views.Fragments.Account.CountryAddressForm, countryFragment);242 }243 @Test244 public void shouldGetAddressBook() throws CMSItemNotFoundException245 {246 BDDMockito.given(userFacade.getAddressBook()).willReturn(Collections.singletonList(addressData));247 final String addressBookPage = accountController.getAddressBook(page);248 Mockito.verify(page).addAttribute("addressData", Collections.singletonList(addressData));249 Mockito.verify(page).addAttribute("cmsPage", contentPageModel);250 Mockito.verify(page).addAttribute("pageTitle", TITLE_FOR_PAGE);251 assertEquals(FULL_VIEW_PATH, addressBookPage);252 }253 @Test254 public void shouldPrepareAddress()255 {256 final AddressForm addressForm = accountController.getPreparedAddressForm();257 assertEquals(FIRST_NAME, addressForm.getFirstName());258 assertEquals(LAST_NAME, addressForm.getLastName());259 assertEquals(TITLE_CODE, addressForm.getTitleCode());260 }261 @Test262 public void shouldGetAddAddress() throws CMSItemNotFoundException263 {264 BDDMockito.given(userFacade.getAddressBook()).willReturn(Collections.singletonList(addressData));265 setupAddressCreateEdit();266 final String addAddressPage = accountController.addAddress(page);267 Mockito.verify(page).addAttribute("countryData", Collections.singletonList(countryData));268 Mockito.verify(page).addAttribute("titleData", Collections.singletonList(titleData));269 Mockito.verify(page).addAttribute("addressForm", addressForm);270 Mockito.verify(page).addAttribute("addressBookEmpty", Boolean.FALSE);271 Mockito.verify(page).addAttribute("isDefaultAddress", Boolean.FALSE);272 assertEquals(FULL_VIEW_PATH, addAddressPage);273 }274 @Test275 public void shouldNotCreateInvalidAddress() throws CMSItemNotFoundException276 {277 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);278 final String addAddressPage = accountController.addAddress(addressForm, bindingResult, page, redirectModel);279 Mockito.verify(accountController).setUpAddressFormAfterError(addressForm, page);280 assertEquals(FULL_VIEW_PATH, addAddressPage);281 }282 @Test283 public void shouldSuggestValidAddress() throws CMSItemNotFoundException284 {285 final AddressVerificationResult<AddressVerificationDecision> avsResult = new AddressVerificationResult<AddressVerificationDecision>();286 avsResult.setDecision(AddressVerificationDecision.REVIEW);287 createBasicAddressFields();288 BDDMockito.given(addressVerificationFacade.verifyAddressData(Mockito.any(AddressData.class))).willReturn(avsResult);289 BDDMockito.given(Boolean.valueOf(290 addressVerificationResultHandler.handleResult(Mockito.eq(avsResult), Mockito.any(AddressData.class), Mockito.eq(page),291 Mockito.eq(redirectModel), Mockito.eq(bindingResult), Mockito.anyBoolean(), Mockito.anyString())))292 .willReturn(Boolean.TRUE);293 final String addAddressPage = accountController.addAddress(addressForm, bindingResult, page, redirectModel);294 assertEquals(FULL_VIEW_PATH, addAddressPage);295 }296 @Test297 public void shouldCreateValidAddress() throws CMSItemNotFoundException298 {299 final AddressVerificationResult<AddressVerificationDecision> avsResult = new AddressVerificationResult<AddressVerificationDecision>();300 avsResult.setDecision(AddressVerificationDecision.ACCEPT);301 createBasicAddressFields();302 BDDMockito.given(addressVerificationFacade.verifyAddressData(Mockito.any(AddressData.class))).willReturn(avsResult);303 BDDMockito.given(Boolean.valueOf(304 addressVerificationResultHandler.handleResult(Mockito.eq(avsResult), Mockito.any(AddressData.class), Mockito.eq(page),305 Mockito.eq(redirectModel), Mockito.eq(bindingResult), Mockito.anyBoolean(), Mockito.anyString())))306 .willReturn(Boolean.FALSE);307 final String addAddressPage = accountController.addAddress(addressForm, bindingResult, page, redirectModel);308 Mockito.verify(userFacade).addAddress(Mockito.any(AddressData.class));309 assertThat(addAddressPage, CoreMatchers.containsString(REDIRECT_TO_EDIT_ADDRESS_PAGE));310 }311 @Test312 public void shouldGetEditAddress() throws CMSItemNotFoundException313 {314 final String addressBookPage = accountController.editAddress(TEST_CODE, page);315 Mockito.verify(page).addAttribute("countryData", checkoutFacade.getCountries(CountryType.SHIPPING));316 Mockito.verify(page).addAttribute("titleData", userFacade.getTitles());317 Mockito.verify(page).addAttribute("addressBookEmpty",318 Boolean.valueOf(CollectionUtils.isEmpty(userFacade.getAddressBook())));319 Mockito.verify(page).addAttribute(ThirdPartyConstants.SeoRobots.META_ROBOTS,320 ThirdPartyConstants.SeoRobots.NOINDEX_NOFOLLOW);321 Mockito.verify(page).addAttribute("edit", Boolean.TRUE);322 assertEquals(FULL_VIEW_PATH, addressBookPage);323 }324 @Test325 public void shouldNotUpdateInvalidAddress() throws CMSItemNotFoundException326 {327 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);328 final String addressBookPage = accountController.editAddress(addressForm, bindingResult, page, redirectModel);329 Mockito.verify(accountController).setUpAddressFormAfterError(addressForm, page);330 assertEquals(FULL_VIEW_PATH, addressBookPage);331 }332 @Test333 public void shouldSuggestValidUpdateAddress() throws CMSItemNotFoundException334 {335 final AddressVerificationResult<AddressVerificationDecision> avsResult = new AddressVerificationResult<AddressVerificationDecision>();336 avsResult.setDecision(AddressVerificationDecision.REVIEW);337 createBasicAddressFields();338 BDDMockito.given(addressVerificationFacade.verifyAddressData(Mockito.any(AddressData.class))).willReturn(avsResult);339 BDDMockito.given(Boolean.valueOf(340 addressVerificationResultHandler.handleResult(Mockito.eq(avsResult), Mockito.any(AddressData.class), Mockito.eq(page),341 Mockito.eq(redirectModel), Mockito.eq(bindingResult), Mockito.anyBoolean(), Mockito.anyString())))342 .willReturn(Boolean.TRUE);343 final String addAddressPage = accountController.editAddress(addressForm, bindingResult, page, redirectModel);344 assertEquals(FULL_VIEW_PATH, addAddressPage);345 }346 @Test347 public void shouldUpdateValidAddress() throws CMSItemNotFoundException348 {349 final String editAddressPage = accountController.editAddress(addressForm, bindingResult, page, redirectModel);350 Mockito.verify(userFacade).editAddress(Mockito.any(AddressData.class));351 assertThat(editAddressPage, CoreMatchers.containsString(REDIRECT_TO_EDIT_ADDRESS_PAGE));352 }353 @Test354 public void shouldSetDefaultAddress()355 {356 final String addressBookPage = accountController.setDefaultAddress(TEST_CODE, redirectModel);357 Mockito.verify(userFacade).setDefaultAddress(Mockito.any(AddressData.class));358 assertEquals(REDIRECT_TO_ADDRESS_BOOK_PAGE, addressBookPage);359 }360 @Test361 public void shouldRemoveAddress()362 {363 final String addressBookPage = accountController.removeAddress(TEST_CODE, redirectModel);364 Mockito.verify(userFacade).removeAddress(Mockito.any(AddressData.class));365 assertEquals(REDIRECT_TO_ADDRESS_BOOK_PAGE, addressBookPage);366 }367 // Orders Tests368 @Test369 public void shouldGetAllOrders() throws CMSItemNotFoundException370 {371 final ShowMode showMode = ShowMode.All;372 setupExistingOrder();373 BDDMockito.given(orderFacade.getPagedOrderHistoryForStatuses(Mockito.any(PageableData.class))).willReturn(searchList);374 final String orderHistoryPage = accountController.orders(1, showMode, "desc", page);375 Mockito.verify(orderFacade).getPagedOrderHistoryForStatuses(Mockito.any(PageableData.class));376 assertEquals(FULL_VIEW_PATH, orderHistoryPage);377 }378 @Test379 public void shouldNotGetNonExistingOrder() throws CMSItemNotFoundException380 {381 BDDMockito.given(orderFacade.getOrderDetailsForCode(TEST_CODE)).willThrow(UnknownIdentifierException.class);382 final String orderHistoryPage = accountController.order(TEST_CODE, page, redirectModel);383 BDDMockito.verify(page, BDDMockito.times(0)).addAttribute(Mockito.anyString(), Mockito.anyString());384 assertEquals(REDIRECT_TO_ORDER_HISTORY_PAGE, orderHistoryPage);385 }386 @Test387 public void shouldGetExistingOrder() throws CMSItemNotFoundException388 {389 setupExistingOrder();390 BDDMockito.given(orderFacade.getOrderDetailsForCode(TEST_CODE)).willReturn(orderData);391 final String myAccountPage = accountController.order(TEST_CODE, page, redirectModel);392 BDDMockito.verify(page, BDDMockito.times(6)).addAttribute(Mockito.anyString(), Mockito.anyString());393 assertEquals(FULL_VIEW_PATH, myAccountPage);394 }395 // Profile Tests396 @Test397 public void shouldGetProfile() throws CMSItemNotFoundException398 {399 final String updateProfilePage = accountController.profile(page);400 BDDMockito.verify(page, BDDMockito.times(7)).addAttribute(Mockito.anyString(), Mockito.anyString());401 assertEquals(FULL_VIEW_PATH, updateProfilePage);402 }403 @Test404 public void shouldGetUpdateProfile() throws CMSItemNotFoundException405 {406 final String profilePage = accountController.editProfile(page);407 BDDMockito.verify(page, BDDMockito.times(7)).addAttribute(Mockito.anyString(), Mockito.anyString());408 assertEquals(FULL_VIEW_PATH, profilePage);409 }410 @Test411 public void shouldNotUpdateProfile() throws CMSItemNotFoundException, DuplicateUidException412 {413 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);414 final String profilePage = accountController.updateProfile(profileForm, bindingResult, page, redirectModel);415 BDDMockito.verify(customerFacade, BDDMockito.times(0)).updateProfile(Mockito.any(CustomerData.class));416 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_PROFILE_CMS_PAGE);417 assertEquals(FULL_VIEW_PATH, profilePage);418 }419 @Test420 public void shouldNotUpdateDuplicateUidProfile() throws CMSItemNotFoundException, DuplicateUidException421 {422 BDDMockito.doThrow(new DuplicateUidException()).when(customerFacade).updateProfile(Mockito.any(CustomerData.class));423 final String profilePage = accountController.updateProfile(profileForm, bindingResult, page, redirectModel);424 BDDMockito.verify(customerFacade).updateProfile(Mockito.any(CustomerData.class));425 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_PROFILE_CMS_PAGE);426 assertEquals(FULL_VIEW_PATH, profilePage);427 }428 @Test429 public void shouldUpdateProfile() throws CMSItemNotFoundException, DuplicateUidException430 {431 final String profilePage = accountController.updateProfile(profileForm, bindingResult, page, redirectModel);432 BDDMockito.verify(customerFacade).updateProfile(Mockito.any(CustomerData.class));433 assertEquals(REDIRECT_TO_UPDATE_PROFILE, profilePage);434 }435 // E-mail Tests436 @Test437 public void shouldGetEmail() throws CMSItemNotFoundException438 {439 final String emailPage = accountController.editEmail(page);440 BDDMockito.verify(page, BDDMockito.times(6)).addAttribute(Mockito.anyString(), Mockito.anyString());441 assertEquals(FULL_VIEW_PATH, emailPage);442 }443 @Test444 public void shouldNotUpdateInavlidEmail() throws CMSItemNotFoundException445 {446 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);447 final String emailUpdatePage = accountController.updateEmail(emailForm, bindingResult, page, redirectModel);448 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_EMAIL_CMS_PAGE);449 assertEquals(FULL_VIEW_PATH, emailUpdatePage);450 }451 @Test452 public void shouldNotUpdateDuplicateEmail() throws CMSItemNotFoundException, PasswordMismatchException, DuplicateUidException453 {454 createEmailForm(EMAIL, FIRST_NAME);455 BDDMockito.doThrow(new DuplicateUidException()).when(customerFacade).changeUid(Mockito.anyString(), Mockito.anyString());456 final String emailUpdatePage = accountController.updateEmail(emailForm, bindingResult, page, redirectModel);457 BDDMockito.verify(bindingResult).rejectValue("email", "profile.email.unique");458 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_EMAIL_CMS_PAGE);459 assertEquals(FULL_VIEW_PATH, emailUpdatePage);460 }461 @Test462 public void shouldNotUpdateInvalidPassword() throws CMSItemNotFoundException, PasswordMismatchException, DuplicateUidException463 {464 createEmailForm(EMAIL, "123");465 BDDMockito.doThrow(new PasswordMismatchException("error")).when(customerFacade).changeUid(Mockito.anyString(),466 Mockito.anyString());467 final String emailUpdatePage = accountController.updateEmail(emailForm, bindingResult, page, redirectModel);468 BDDMockito.verify(bindingResult).rejectValue("password", "profile.currentPassword.invalid");469 BDDMockito.verify(accountController).setErrorMessagesAndCMSPage(page, UPDATE_EMAIL_CMS_PAGE);470 assertEquals(FULL_VIEW_PATH, emailUpdatePage);471 }472 // Password Tests473 @Test474 public void shouldGetPassword() throws CMSItemNotFoundException475 {476 final String passwordPage = accountController.updatePassword(page);477 BDDMockito.verify(page).addAttribute(BDDMockito.eq("updatePasswordForm"), Mockito.any(UpdatePasswordForm.class));478 assertEquals(FULL_VIEW_PATH, passwordPage);479 }480 @Test481 public void shouldNotUpdatePassword() throws CMSItemNotFoundException482 {483 BDDMockito.given(Boolean.valueOf(bindingResult.hasErrors())).willReturn(Boolean.TRUE);484 BDDMockito.given(accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile.updatePasswordForm"))485 .willReturn(breadcrumbsList);486 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);487 BDDMockito.verify(page).addAttribute("breadcrumbs", breadcrumbsList);488 assertEquals(FULL_VIEW_PATH, passwordPage);489 }490 @Test491 public void shouldNotUpdatePasswordNotEqual() throws CMSItemNotFoundException492 {493 BDDMockito.given(passwordForm.getCurrentPassword()).willReturn(TEST_CODE);494 BDDMockito.given(passwordForm.getNewPassword()).willReturn("Different");495 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);496 BDDMockito.verify(bindingResult).rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},497 "validation.checkPwd.equals");498 assertEquals(REDIRECT_TO_PASSWORD_UPDATE_PAGE, passwordPage);499 }500 @Test501 public void shouldNotUpdatePasswordNotValid() throws CMSItemNotFoundException502 {503 BDDMockito.given(passwordForm.getCheckNewPassword()).willReturn(TEST_CODE);504 BDDMockito.given(passwordForm.getNewPassword()).willReturn(TEST_CODE);505 Mockito.doThrow(new PasswordMismatchException("error")).when(customerFacade).changePassword(Mockito.anyString(),506 Mockito.anyString());507 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);508 BDDMockito.verify(bindingResult).rejectValue("currentPassword", "profile.currentPassword.invalid", new Object[] {},509 "profile.currentPassword.invalid");510 assertEquals(REDIRECT_TO_PASSWORD_UPDATE_PAGE, passwordPage);511 }512 @Test513 public void shouldUpdatePassword() throws CMSItemNotFoundException514 {515 BDDMockito.given(passwordForm.getCheckNewPassword()).willReturn(TEST_CODE);516 BDDMockito.given(passwordForm.getNewPassword()).willReturn(TEST_CODE);517 BDDMockito.given(passwordForm.getCurrentPassword()).willReturn(TEST_CODE);518 final String passwordPage = accountController.updatePassword(passwordForm, bindingResult, page, redirectModel);519 BDDMockito.verify(customerFacade).changePassword(TEST_CODE, TEST_CODE);520 assertEquals(REDIRECT_TO_PASSWORD_UPDATE_PAGE, passwordPage);521 }522 // Payment Tests523 @Test524 public void shouldGetPaymentDetails() throws CMSItemNotFoundException525 {526 final String paymentDetailsPage = accountController.paymentDetails(page);527 BDDMockito.verify(page, BDDMockito.times(7)).addAttribute(Mockito.anyString(), Mockito.anyString());528 assertEquals(FULL_VIEW_PATH, paymentDetailsPage);529 }530 @Test531 public void shouldSetDefaultPaymentDetailsNull() throws CMSItemNotFoundException532 {533 final String paymentDetailsPage = accountController.setDefaultPaymentDetails(null);534 BDDMockito.verify(userFacade, BDDMockito.times(0)).getCCPaymentInfoForCode(Mockito.anyString());535 BDDMockito.verify(userFacade).setDefaultPaymentInfo(Mockito.any(CCPaymentInfoData.class));536 assertEquals(REDIRECT_TO_PAYMENT_INFO_PAGE, paymentDetailsPage);537 }538 @Test539 public void shouldSetDefaultPaymentDetails() throws CMSItemNotFoundException540 {541 final String paymentDetailsPage = accountController.setDefaultPaymentDetails(TEST_CODE);542 BDDMockito.verify(userFacade, BDDMockito.times(1)).getCCPaymentInfoForCode(Mockito.anyString());543 BDDMockito.verify(userFacade).setDefaultPaymentInfo(Mockito.any(CCPaymentInfoData.class));544 assertEquals(REDIRECT_TO_PAYMENT_INFO_PAGE, paymentDetailsPage);545 }546 @Test547 public void shouldRemovePaymentMethod() throws CMSItemNotFoundException548 {549 final String paymentDetailsPage = accountController.removePaymentMethod(TEST_CODE, redirectModel);550 BDDMockito.verify(userFacade, BDDMockito.times(1)).unlinkCCPaymentInfo(Mockito.anyString());551 assertEquals(REDIRECT_TO_PAYMENT_INFO_PAGE, paymentDetailsPage);552 }553}...
should
Using AI Code Generation
1import static org.mockito.BDDMockito.given2import static org.mockito.BDDMockito.then3import static org.mockito.BDDMockito.willDoNothing4import static org.mockito.BDDMockito.willReturn5import org.junit.jupiter.api.Test6import org.junit.jupiter.api.extension.ExtendWith7import org.mockito.Mock8import org.mockito.junit.jupiter.MockitoExtension9@ExtendWith(MockitoExtension::class)10class BDDMockitoTest {11 fun `should use BDDMockito`() {12 given(mockedList[0]).willReturn("first")13 then(mockedList).should().get(0)14 then(mockedList).shouldHaveNoMoreInteractions()15 assert(element == "first")16 }17 fun `should use BDDMockito with willDoNothing`() {18 willDoNothing().given(mockedList).clear()19 mockedList.clear()20 then(mockedList).should().clear()21 then(mockedList).shouldHaveNoMoreInteractions()22 }23 fun `should use BDDMockito with willReturn`() {24 willReturn("first").given(mockedList)[0]25 then(mockedList).should().get(0)26 then(mockedList).shouldHaveNoMoreInteractions()27 assert(element == "first")28 }29}
should
Using AI Code Generation
1List<String> mockedList = mock(List.class);2given(mockedList.get(0)).willReturn("first");3given(mockedList.get(1)).willReturn("second");4given(mockedList.get(2)).willThrow(new RuntimeException());5given(mockedList.get(3)).willAnswer(invocation -> "third");6given(mockedList.get(4)).willCallRealMethod();7given(mockedList.get(5)).willDoNothing();8given(mockedList.get(6)).will(invocation -> "sixth");9given(mockedList.get(7)).will(invocation -> {10 return "seventh";11});12given(mockedList.get(8)).will(invocation -> {13 return "eighth";14});15given(mockedList.get(9)).will(invocation -> {16 return "ninth";17});18given(mockedList.get(10)).will(invocation -> {19 return "tenth";20});21given(mockedList.get(11)).will(invocation -> {22 return "eleventh";23});24given(mockedList.get(12)).will(invocation -> {
should
Using AI Code Generation
1List<String> mockedList = mock(List.class);2given(mockedList.get(0)).willReturn("first");3given(mockedList.get(1)).willReturn("second");4System.out.println(mockedList.get(0));5System.out.println(mockedList.get(1));6System.out.println(mockedList.get(999));7verify(mockedList).get(0);8verify(mockedList).get(1);9verify(mockedList).get(999);10given(mockedList.get(anyInt())).willThrow(new RuntimeException());11given(mockedList.contains(argThat(isValid()))).willReturn(true);12System.out.println(mockedList.contains("invalid arg"));13verify(mockedList).get(anyInt());14verify(mockedList).add(argThat(isValid()));15verify(mockedList).add(argThat(s -> s.length() > 5));16verify(mockedList).add(argThat(s -> s.length() > 5 && s.startsWith("foo")));17given(mockedList.contains("one")).willReturn(true);18given(mockedList.contains("two")).willReturn(true);19System.out.println(mockedList.contains("one"));20verify(mockedList).contains("one");21verify(mockedList).contains(argThat(s -> s.length() > 5));22verify(mockedList).contains(argThat(s -> s.length() > 5 && s.startsWith("foo")));23verify(mockedList).add(anyString());
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!