How to use doThrow method of org.mockito.Mockito class

Best Mockito code snippet using org.mockito.Mockito.doThrow

Source:UserRepositoryTest.java Github

copy

Full Screen

...121 @Test122 public void addNewUserWhenFirstNameIsNullConstraintShouldThrowConstraintViolation() {123 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);124 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);125 Mockito.doThrow(cve).when(session).save(user);126 try {127 user.setFirstName(null);128 userRepository.addUser(user);129 } catch (ConstraintViolationException e) {130 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());131 } catch (Exception e) {132 fail("We should only have a constraint violation.");133 }134 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));135 Mockito.verifyNoMoreInteractions(session);136 }137 138 @Test139 public void addNewUserWhenLastNameIsNullConstraintShouldThrowConstraintViolation() {140 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);141 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);142 Mockito.doThrow(cve).when(session).save(user);143 try {144 user.setLastName(null);145 userRepository.addUser(user);146 } catch (ConstraintViolationException e) {147 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());148 } catch (Exception e) {149 fail("We should only have a constraint violation.");150 }151 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));152 Mockito.verifyNoMoreInteractions(session);153 }154 155 @Test156 public void addNewUserWhenUserameIsNullConstraintShouldThrowConstraintViolation() {157 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);158 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);159 Mockito.doThrow(cve).when(session).save(user);160 try {161 user.setUsername(null);162 userRepository.addUser(user);163 } catch (ConstraintViolationException e) {164 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());165 } catch (Exception e) {166 fail("We should only have a constraint violation.");167 }168 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));169 Mockito.verifyNoMoreInteractions(session);170 }171 172 @Test173 public void addNewUserWhenShortUsernameIsNullConstraintShouldThrowConstraintViolation() {174 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);175 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);176 Mockito.doThrow(cve).when(session).save(user);177 try {178 user.setShortUsername(null);179 userRepository.addUser(user);180 } catch (ConstraintViolationException e) {181 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());182 } catch (Exception e) {183 fail("We should only have a constraint violation.");184 }185 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));186 Mockito.verifyNoMoreInteractions(session);187 }188 189 @Test190 public void addNewUserWhenPasswordIsNullConstraintShouldThrowConstraintViolation() {191 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);192 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);193 Mockito.doThrow(cve).when(session).save(user);194 try {195 user.setPassword(null);196 userRepository.addUser(user);197 } catch (ConstraintViolationException e) {198 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());199 } catch (Exception e) {200 fail("We should only have a constraint violation.");201 }202 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));203 Mockito.verifyNoMoreInteractions(session);204 }205 /*206 * END TEST OF NULL CONSTRAINTS207 */208 209 /**210 * Add New User Tests: Test All Empty Constraint Violations.211 */212 @Test213 public void addNewUserWhenFirstNameIsEmptyConstraintShouldThrowConstraintViolation() {214 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);215 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);216 Mockito.doThrow(cve).when(session).save(user);217 try {218 user.setFirstName("");219 userRepository.addUser(user);220 } catch (ConstraintViolationException e) {221 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());222 } catch (Exception e) {223 fail("We should only have a constraint violation.");224 }225 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));226 Mockito.verifyNoMoreInteractions(session);227 }228 229 @Test230 public void addNewUserWhenLastNameIsEmptyConstraintShouldThrowConstraintViolation() {231 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);232 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);233 Mockito.doThrow(cve).when(session).save(user);234 try {235 user.setLastName("");236 userRepository.addUser(user);237 } catch (ConstraintViolationException e) {238 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());239 } catch (Exception e) {240 fail("We should only have a constraint violation.");241 }242 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));243 Mockito.verifyNoMoreInteractions(session);244 }245 246 @Test247 public void addNewUserWhenUserameIsEmptyConstraintShouldThrowConstraintViolation() {248 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);249 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);250 Mockito.doThrow(cve).when(session).save(user);251 try {252 user.setUsername("");253 userRepository.addUser(user);254 } catch (ConstraintViolationException e) {255 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());256 } catch (Exception e) {257 fail("We should only have a constraint violation.");258 }259 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));260 Mockito.verifyNoMoreInteractions(session);261 }262 263 @Test264 public void addNewUserWhenShortUsernameIsEmptyConstraintShouldThrowConstraintViolation() {265 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);266 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);267 Mockito.doThrow(cve).when(session).save(user);268 try {269 user.setShortUsername("");270 userRepository.addUser(user);271 } catch (ConstraintViolationException e) {272 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());273 } catch (Exception e) {274 fail("We should only have a constraint violation.");275 }276 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));277 Mockito.verifyNoMoreInteractions(session);278 }279 280 @Test281 public void addNewUserWhenPasswordIsEmptyConstraintShouldThrowConstraintViolation() {282 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);283 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);284 Mockito.doThrow(cve).when(session).save(user);285 try {286 user.setPassword("");287 userRepository.addUser(user);288 } catch (ConstraintViolationException e) {289 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());290 } catch (Exception e) {291 fail("We should only have a constraint violation.");292 }293 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));294 Mockito.verifyNoMoreInteractions(session);295 }296 /*297 * END TEST OF EMPTY CONSTRAINTS298 */299 300 /**301 * Add New User Tests: Test All Length Constraint Violations.302 */303 @Test304 public void addNewUserWhenFirstNameExceedsLengthConstraintShouldThrowConstraintViolation() {305 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);306 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);307 Mockito.doThrow(cve).when(session).save(user);308 try {309 user.setFirstName(this.THIRTY_ONE_CHARACTERS);310 userRepository.addUser(user);311 } catch (ConstraintViolationException e) {312 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());313 } catch (Exception e) {314 fail("We should only have a constraint violation.");315 }316 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));317 Mockito.verifyNoMoreInteractions(session);318 }319 320 @Test321 public void addNewUserWhenLastNameExceedsLengthConstraintShouldThrowConstraintViolation() {322 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);323 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);324 Mockito.doThrow(cve).when(session).save(user);325 try {326 user.setLastName(this.THIRTY_ONE_CHARACTERS);327 userRepository.addUser(user);328 } catch (ConstraintViolationException e) {329 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());330 } catch (Exception e) {331 fail("We should only have a constraint violation.");332 }333 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));334 Mockito.verifyNoMoreInteractions(session);335 }336 337 @Test338 public void addNewUserWhenUserameExceedsLengthConstraintShouldThrowConstraintViolation() {339 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);340 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);341 Mockito.doThrow(cve).when(session).save(user);342 try {343 user.setUsername(this.FIFTY_ONE_CHARACTERS);344 userRepository.addUser(user);345 } catch (ConstraintViolationException e) {346 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());347 } catch (Exception e) {348 fail("We should only have a constraint violation.");349 }350 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));351 Mockito.verifyNoMoreInteractions(session);352 }353 354 @Test355 public void addNewUserWhenShortUsernameExceedsLengthConstraintShouldThrowConstraintViolation() {356 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);357 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);358 Mockito.doThrow(cve).when(session).save(user);359 try {360 user.setShortUsername(this.FORTY_ONE_CHARACTERS);361 userRepository.addUser(user);362 } catch (ConstraintViolationException e) {363 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());364 } catch (Exception e) {365 fail("We should only have a constraint violation.");366 }367 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));368 Mockito.verifyNoMoreInteractions(session);369 }370 371 @Test372 public void addNewUserWhenPasswordExceedsLengthConstraintShouldThrowConstraintViolation() {373 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);374 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);375 Mockito.doThrow(cve).when(session).save(user);376 try {377 user.setPassword(this.TWO_HUNDRED_FIFTY_SIX_CHARACTERS);378 userRepository.addUser(user);379 } catch (ConstraintViolationException e) {380 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());381 } catch (Exception e) {382 fail("We should only have a constraint violation.");383 }384 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));385 Mockito.verifyNoMoreInteractions(session);386 }387 /*388 * END TEST OF LENGTH CONSTRAINTS389 */390 391 /**392 * Add New User Tests: Test All Email Constraint Violations.393 */394 @Test395 public void addNewUserWhenUserameIsNotAEmailConstraintShouldThrowConstraintViolation() {396 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);397 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);398 Mockito.doThrow(cve).when(session).save(user);399 try {400 user.setUsername(this.THIRTY_ONE_CHARACTERS);401 userRepository.addUser(user);402 } catch (ConstraintViolationException e) {403 assertEquals("Constraint Violation: Could not add new user ... ", e.getMessage());404 } catch (Exception e) {405 fail("We should only have a constraint violation.");406 }407 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));408 Mockito.verifyNoMoreInteractions(session);409 }410 /*411 * END TEST OF EMAIL CONSTRAINTS412 */413 414 /**415 * Add New User Tests: Add New User is successful.416 * @throws Exception 417 */418 419 @Test420 public void addNewUserWhenUserIsValid() { 421 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);422 Mockito.when(session.save(user)).thenReturn(true);423 try {424 userRepository.addUser(user);425 } catch (Exception e) {426 fail("User should add successfully");427 }428 Mockito.verify(session, VerificationModeFactory.times(1)).save(Mockito.any(User.class));429 Mockito.verifyNoMoreInteractions(session);430 }431 432 /**433 * Update User Tests: Should cover throwing a new Exception.434 * 435 * @throws ConstraintViolationException436 * @throws Exception437 */438 @Test(expected = Exception.class)439 public void updateUserWhenUserIsNullShouldThrowException() throws ConstraintViolationException, Exception {440 userRepository.updateUser(null);441 }442 443 /**444 * Update User Tests: Test All Null Constraint Violations.445 */446 @Test447 public void updateUserWhenFirstNameIsNullConstraintShouldThrowConstraintViolation() {448 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);449 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);450 Mockito.doThrow(cve).when(session).update(user);451 try {452 user.setFirstName(null);453 userRepository.updateUser(user);454 } catch (ConstraintViolationException e) {455 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());456 } catch (Exception e) {457 fail("We should only have a constraint violation.");458 }459 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));460 Mockito.verifyNoMoreInteractions(session);461 }462 463 @Test464 public void updateUserWhenLastNameIsNullConstraintShouldThrowConstraintViolation() {465 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);466 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);467 Mockito.doThrow(cve).when(session).update(user);468 try {469 user.setLastName(null);470 userRepository.updateUser(user);471 } catch (ConstraintViolationException e) {472 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());473 } catch (Exception e) {474 fail("We should only have a constraint violation.");475 }476 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));477 Mockito.verifyNoMoreInteractions(session);478 }479 480 @Test481 public void updateUserWhenUserameIsNullConstraintShouldThrowConstraintViolation() {482 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);483 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);484 Mockito.doThrow(cve).when(session).update(user);485 try {486 user.setUsername(null);487 userRepository.updateUser(user);488 } catch (ConstraintViolationException e) {489 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());490 } catch (Exception e) {491 fail("We should only have a constraint violation.");492 }493 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));494 Mockito.verifyNoMoreInteractions(session);495 }496 497 @Test498 public void updateUserWhenShortUsernameIsNullConstraintShouldThrowConstraintViolation() {499 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);500 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);501 Mockito.doThrow(cve).when(session).update(user);502 try {503 user.setShortUsername(null);504 userRepository.updateUser(user);505 } catch (ConstraintViolationException e) {506 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());507 } catch (Exception e) {508 fail("We should only have a constraint violation.");509 }510 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));511 Mockito.verifyNoMoreInteractions(session);512 }513 514 @Test515 public void updateUserWhenPasswordIsNullConstraintShouldThrowConstraintViolation() {516 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);517 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);518 Mockito.doThrow(cve).when(session).update(user);519 try {520 user.setPassword(null);521 userRepository.updateUser(user);522 } catch (ConstraintViolationException e) {523 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());524 } catch (Exception e) {525 fail("We should only have a constraint violation.");526 }527 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));528 Mockito.verifyNoMoreInteractions(session);529 }530 /*531 * END TEST OF NULL CONSTRAINTS532 */533 534 /**535 * Update User Tests: Test All Empty Constraint Violations.536 */537 @Test538 public void updateUserWhenFirstNameIsEmptyConstraintShouldThrowConstraintViolation() {539 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);540 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);541 Mockito.doThrow(cve).when(session).update(user);542 try {543 user.setFirstName("");544 userRepository.updateUser(user);545 } catch (ConstraintViolationException e) {546 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());547 } catch (Exception e) {548 fail("We should only have a constraint violation.");549 }550 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));551 Mockito.verifyNoMoreInteractions(session);552 }553 554 @Test555 public void updateUserWhenLastNameIsEmptyConstraintShouldThrowConstraintViolation() {556 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);557 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);558 Mockito.doThrow(cve).when(session).update(user);559 try {560 user.setLastName("");561 userRepository.updateUser(user);562 } catch (ConstraintViolationException e) {563 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());564 } catch (Exception e) {565 fail("We should only have a constraint violation.");566 }567 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));568 Mockito.verifyNoMoreInteractions(session);569 }570 571 @Test572 public void updateUserWhenUserameIsEmptyConstraintShouldThrowConstraintViolation() {573 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);574 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);575 Mockito.doThrow(cve).when(session).update(user);576 try {577 user.setUsername("");578 userRepository.updateUser(user);579 } catch (ConstraintViolationException e) {580 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());581 } catch (Exception e) {582 fail("We should only have a constraint violation.");583 }584 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));585 Mockito.verifyNoMoreInteractions(session);586 }587 588 @Test589 public void updateUserWhenShortUsernameIsEmptyConstraintShouldThrowConstraintViolation() {590 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);591 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);592 Mockito.doThrow(cve).when(session).update(user);593 try {594 user.setShortUsername("");595 userRepository.updateUser(user);596 } catch (ConstraintViolationException e) {597 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());598 } catch (Exception e) {599 fail("We should only have a constraint violation.");600 }601 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));602 Mockito.verifyNoMoreInteractions(session);603 }604 605 @Test606 public void updateUserWhenPasswordIsEmptyConstraintShouldThrowConstraintViolation() {607 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);608 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);609 Mockito.doThrow(cve).when(session).update(user);610 try {611 user.setPassword("");612 userRepository.updateUser(user);613 } catch (ConstraintViolationException e) {614 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());615 } catch (Exception e) {616 fail("We should only have a constraint violation.");617 }618 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));619 Mockito.verifyNoMoreInteractions(session);620 }621 /*622 * END TEST OF EMPTY CONSTRAINTS623 */624 625 /**626 * Update User Tests: Test All Length Constraint Violations.627 */628 @Test629 public void updateUserWhenFirstNameExceedsLengthConstraintShouldThrowConstraintViolation() {630 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);631 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);632 Mockito.doThrow(cve).when(session).update(user);633 try {634 user.setFirstName(this.THIRTY_ONE_CHARACTERS);635 userRepository.updateUser(user);636 } catch (ConstraintViolationException e) {637 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());638 } catch (Exception e) {639 fail("We should only have a constraint violation.");640 }641 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));642 Mockito.verifyNoMoreInteractions(session);643 }644 645 @Test646 public void updateUserWhenLastNameExceedsLengthConstraintShouldThrowConstraintViolation() {647 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);648 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);649 Mockito.doThrow(cve).when(session).update(user);650 try {651 user.setLastName(this.THIRTY_ONE_CHARACTERS);652 userRepository.updateUser(user);653 } catch (ConstraintViolationException e) {654 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());655 } catch (Exception e) {656 fail("We should only have a constraint violation.");657 }658 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));659 Mockito.verifyNoMoreInteractions(session);660 }661 662 @Test663 public void updateUserWhenUserameExceedsLengthConstraintShouldThrowConstraintViolation() {664 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);665 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);666 Mockito.doThrow(cve).when(session).update(user);667 try {668 user.setUsername(this.FIFTY_ONE_CHARACTERS);669 userRepository.updateUser(user);670 } catch (ConstraintViolationException e) {671 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());672 } catch (Exception e) {673 fail("We should only have a constraint violation.");674 }675 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));676 Mockito.verifyNoMoreInteractions(session);677 }678 679 @Test680 public void updateUserWhenShortUsernameExceedsLengthConstraintShouldThrowConstraintViolation() {681 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);682 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);683 Mockito.doThrow(cve).when(session).update(user);684 try {685 user.setShortUsername(this.FORTY_ONE_CHARACTERS);686 userRepository.updateUser(user);687 } catch (ConstraintViolationException e) {688 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());689 } catch (Exception e) {690 fail("We should only have a constraint violation.");691 }692 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));693 Mockito.verifyNoMoreInteractions(session);694 }695 696 @Test697 public void updateUserWhenPasswordExceedsLengthConstraintShouldThrowConstraintViolation() {698 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);699 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);700 Mockito.doThrow(cve).when(session).update(user);701 try {702 user.setPassword(this.TWO_HUNDRED_FIFTY_SIX_CHARACTERS);703 userRepository.updateUser(user);704 } catch (ConstraintViolationException e) {705 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());706 } catch (Exception e) {707 fail("We should only have a constraint violation.");708 }709 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));710 Mockito.verifyNoMoreInteractions(session);711 }712 /*713 * END TEST OF LENGTH CONSTRAINTS714 */715 716 /**717 * Update User Tests: Test All Email Constraint Violations.718 */719 @Test720 public void updateUserWhenUserameIsNotAEmailConstraintShouldThrowConstraintViolation() {721 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);722 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);723 Mockito.doThrow(cve).when(session).update(user);724 try {725 user.setUsername(this.THIRTY_ONE_CHARACTERS);726 userRepository.updateUser(user);727 } catch (ConstraintViolationException e) {728 assertEquals("Constraint Violation: Could not update user ... ", e.getMessage());729 } catch (Exception e) {730 fail("We should only have a constraint violation.");731 }732 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));733 Mockito.verifyNoMoreInteractions(session);734 }735 /*736 * END TEST OF EMAIL CONSTRAINTS737 */738 739 /**740 * Update User Tests: Add New User is successful.741 * @throws Exception 742 */743 744 @Test745 public void updateUserWhenUserIsValid() { 746 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);747 Mockito.doNothing().when(session).update(user);748 try {749 userRepository.updateUser(user);750 } catch (Exception e) {751 fail("User should add successfully");752 }753 Mockito.verify(session, VerificationModeFactory.times(1)).update(Mockito.any(User.class));754 Mockito.verifyNoMoreInteractions(session);755 }756 757 /**758 * Delete User Tests: Should cover throwing a new Exception.759 * 760 * @throws Exception761 */762 @Test(expected = Exception.class)763 public void deleteUserWhenUserIsNullShouldThrowException() throws Exception {764 userRepository.deleteUser(null);765 }766 767 @Test768 public void deleteUserWhenUserIsValid() {769 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);770 Mockito.doNothing().when(session).delete(user);771 try {772 userRepository.deleteUser(user);773 } catch (Exception e) {774 fail("User should add successfully");775 }776 Mockito.verify(session, VerificationModeFactory.times(1)).delete(Mockito.any(User.class));777 Mockito.verifyNoMoreInteractions(session);778 }779 780 @SuppressWarnings("unchecked")781 @Test(expected = RuntimeException.class)782 public void findUserByIdThrowsRuntimeException() throws RuntimeException {783 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);784 Mockito.doThrow(new RuntimeException()).when(session).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());785 786 userRepository.findById(1L);787 788 Mockito.verify(session, VerificationModeFactory.times(1)).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());789 Mockito.verifyNoMoreInteractions(session);790 }791 792 @SuppressWarnings("unchecked")793 @Test(expected = HibernateException.class)794 public void findUserByIdThrowsHibernateException() throws HibernateException {795 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);796 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(session).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());797 798 userRepository.findById(1L);799 800 Mockito.verify(session, VerificationModeFactory.times(1)).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());801 Mockito.verifyNoMoreInteractions(session);802 }803 804 @SuppressWarnings("unchecked")805 @Test806 public void findUserByIdIsSuccessful() {807 user.setId(1L);808 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);809 Mockito.doReturn(user).when(session).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());810 811 try {812 User u = userRepository.findById(1L);813 814 assertNotNull(u);815 } catch (Exception e) {816 fail("No Exceptions should be thrown: user should not be null.");817 }818 Mockito.verify(session, VerificationModeFactory.times(1)).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());819 Mockito.verifyNoMoreInteractions(session);820 }821 822 @SuppressWarnings("unchecked")823 @Test824 public void findUserByIdFails(){825 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);826 Mockito.doReturn(null).when(session).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());827 828 try {829 User u = userRepository.findById(1L);830 831 assertNull(u);832 } catch (Exception e) {833 fail("No Exceptions should be thrown: user should be null.");834 }835 Mockito.verify(session, VerificationModeFactory.times(1)).get((Class<User>) Mockito.anyObject(), Mockito.anyLong());836 Mockito.verifyNoMoreInteractions(session);837 }838 839 @Test(expected = RuntimeException.class)840 public void findUserByUsernameThrowsRuntimeException() throws RuntimeException {841 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);842 Mockito.doThrow(new RuntimeException()).when(session).createQuery(Mockito.anyString());843 844 userRepository.findByUsername("test@fake.com");845 846 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());847 Mockito.verifyNoMoreInteractions(session);848 }849 850 @Test(expected = HibernateException.class)851 public void findUserByUsernameThrowsHibernateException() throws HibernateException {852 List<User> lst = new ArrayList<User>();853 lst.add(user);854 855 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);856 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(session).createQuery(Mockito.anyString());857 858 userRepository.findByUsername("test@fake.com");859 860 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());861 Mockito.verifyNoMoreInteractions(session);862 }863 864 @Test865 public void findUserByUsernameIsSuccessful() {866 List<User> lst = new ArrayList<User>();867 lst.add(user);868 869 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);870 Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(query);871 Mockito.when(query.list()).thenReturn(lst);872 873 try {874 User u = userRepository.findByUsername("test@fake.com");875 876 assertNotNull(u);877 } catch (Exception e) {878 fail("No Exceptions should be thrown: user should not be null.");879 }880 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());881 Mockito.verifyNoMoreInteractions(session);882 }883 884 @Test885 public void findUserByUsernameFailsWhenQueryReturnsNull(){886 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);887 Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(query);888 Mockito.when(query.list()).thenReturn(null);889 890 try {891 User u = userRepository.findByUsername("test@fake.com");892 893 assertNull(u);894 } catch (Exception e) {895 fail("No Exceptions should be thrown: user should be null.");896 }897 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());898 Mockito.verifyNoMoreInteractions(session);899 }900 901 @Test902 public void findUserByUsernameFailsWhenQueryReturnsMultipleResults(){903 List<User> lst = new ArrayList<User>();904 user.setId(1L);905 lst.add(user);906 user.setId(2L);907 lst.add(user);908 assertEquals(lst.size(), 2);909 910 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);911 Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(query);912 Mockito.when(query.list()).thenReturn(lst);913 914 try {915 User u = userRepository.findByUsername("test@fake.com");916 917 assertNull(u);918 } catch (Exception e) {919 fail("No Exceptions should be thrown: user should be null.");920 }921 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());922 Mockito.verifyNoMoreInteractions(session);923 }924 925 @Test(expected = RuntimeException.class)926 public void findUsersByRoleThrowsRuntimeException() throws RuntimeException {927 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);928 Mockito.doThrow(new RuntimeException()).when(session).createQuery(Mockito.anyString());929 930 userRepository.findUsersByRole(1L);931 932 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());933 Mockito.verifyNoMoreInteractions(session);934 }935 936 @Test(expected = HibernateException.class)937 public void findUsersByRoleThrowsHibernateException() throws HibernateException {938 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);939 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(session).createQuery(Mockito.anyString());940 941 userRepository.findUsersByRole(1L);942 943 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());944 Mockito.verifyNoMoreInteractions(session);945 }946 947 @Test948 public void findUsersByRoleIsSuccessful() {949 List<User> lst = new ArrayList<User>();950 lst.add(user);951 952 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);953 Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(query);954 Mockito.when(query.list()).thenReturn(lst);955 956 try {957 List<User> u = userRepository.findUsersByRole(1L);958 959 assertNotNull(u);960 } catch (Exception e) {961 fail("No Exceptions should be thrown: user should not be null.");962 }963 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());964 Mockito.verifyNoMoreInteractions(session);965 }966 967 @Test968 public void findUsersByRoleFails(){969 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);970 Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(query);971 Mockito.when(query.list()).thenReturn(null);972 973 try {974 List<User> u = userRepository.findUsersByRole(1L);975 976 assertNull(u);977 } catch (Exception e) {978 fail("No Exceptions should be thrown: user should be null.");979 }980 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());981 Mockito.verifyNoMoreInteractions(session);982 }983 984 @Test(expected = RuntimeException.class)985 public void findAllUsersThrowsRuntimeException() throws RuntimeException {986 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);987 Mockito.doThrow(new RuntimeException()).when(session).createQuery(Mockito.anyString());988 989 userRepository.findAllUsers();990 991 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());992 Mockito.verifyNoMoreInteractions(session);993 }994 995 @Test(expected = HibernateException.class)996 public void findAllUsersThrowsHibernateException() throws HibernateException {997 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);998 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(session).createQuery(Mockito.anyString());999 1000 userRepository.findAllUsers();1001 1002 Mockito.verify(session, VerificationModeFactory.times(1)).createQuery(Mockito.anyString());1003 Mockito.verifyNoMoreInteractions(session);1004 }1005 1006 @Test1007 public void findAllUsersIsSuccessful() {1008 List<User> lst = new ArrayList<User>();1009 lst.add(user);1010 1011 Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);1012 Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(query); ...

Full Screen

Full Screen

Source:UserServiceTest.java Github

copy

Full Screen

...113 */114 @Test(expected = ConstraintViolationException.class)115 public void addNewUserWhenFirstNameIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {116 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);117 Mockito.doThrow(cve).when(userRepository).addUser(user);118 119 user.setFirstName(null);120 userService.addUser(user);121 122 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));123 Mockito.verifyNoMoreInteractions(userRepository);124 }125 126 @Test(expected = ConstraintViolationException.class)127 public void addNewUserWhenLastNameIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {128 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);129 Mockito.doThrow(cve).when(userRepository).addUser(user);130 131 user.setLastName(null);132 userService.addUser(user);133 134 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));135 Mockito.verifyNoMoreInteractions(userRepository);136 }137 138 @Test(expected = ConstraintViolationException.class)139 public void addNewUserWhenUserameIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {140 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);141 Mockito.doThrow(cve).when(userRepository).addUser(user);142 143 user.setUsername(null);144 userService.addUser(user);145 146 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));147 Mockito.verifyNoMoreInteractions(userRepository);148 }149 150 @Test(expected = ConstraintViolationException.class)151 public void addNewUserWhenShortUsernameIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {152 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);153 Mockito.doThrow(cve).when(userRepository).addUser(user);154 155 user.setShortUsername(null);156 userService.addUser(user);157 158 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));159 Mockito.verifyNoMoreInteractions(userRepository);160 }161 162 @Test(expected = ConstraintViolationException.class)163 public void addNewUserWhenPasswordIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {164 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);165 Mockito.doThrow(cve).when(userRepository).addUser(user);166 167 user.setPassword(null);168 userService.addUser(user);169 170 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));171 Mockito.verifyNoMoreInteractions(userRepository);172 }173 /*174 * END TEST OF NULL CONSTRAINTS175 */176 177 /**178 * Add New User Tests: Test All Empty Constraint Violations.179 * @throws Exception 180 * @throws ConstraintViolationException 181 */182 @Test(expected = ConstraintViolationException.class)183 public void addNewUserWhenFirstNameIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {184 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);185 Mockito.doThrow(cve).when(userRepository).addUser(user);186 187 user.setFirstName("");188 userService.addUser(user);189 190 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));191 Mockito.verifyNoMoreInteractions(userRepository);192 }193 194 @Test(expected = ConstraintViolationException.class)195 public void addNewUserWhenLastNameIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {196 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);197 Mockito.doThrow(cve).when(userRepository).addUser(user);198 199 user.setLastName("");200 userService.addUser(user);201 202 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));203 Mockito.verifyNoMoreInteractions(userRepository);204 }205 206 @Test(expected = ConstraintViolationException.class)207 public void addNewUserWhenUserameIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {208 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);209 Mockito.doThrow(cve).when(userRepository).addUser(user);210 211 user.setUsername("");212 userService.addUser(user);213 214 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));215 Mockito.verifyNoMoreInteractions(userRepository);216 }217 218 @Test(expected = ConstraintViolationException.class)219 public void addNewUserWhenShortUsernameIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {220 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);221 Mockito.doThrow(cve).when(userRepository).addUser(user);222 223 user.setShortUsername("");224 userService.addUser(user);225 226 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));227 Mockito.verifyNoMoreInteractions(userRepository);228 }229 230 @Test(expected = ConstraintViolationException.class)231 public void addNewUserWhenPasswordIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {232 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);233 Mockito.doThrow(cve).when(userRepository).addUser(user);234 235 user.setPassword("");236 userService.addUser(user);237 238 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));239 Mockito.verifyNoMoreInteractions(userRepository);240 }241 /*242 * END TEST OF EMPTY CONSTRAINTS243 */244 245 /**246 * Add New User Tests: Test All Length Constraint Violations.247 * @throws Exception 248 * @throws javax.validation.ConstraintViolationException 249 */250 @Test(expected = ConstraintViolationException.class)251 public void addNewUserWhenFirstNameExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {252 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);253 Mockito.doThrow(cve).when(userRepository).addUser(user);254 255 user.setFirstName(this.THIRTY_ONE_CHARACTERS);256 userService.addUser(user);257 258 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));259 Mockito.verifyNoMoreInteractions(userRepository);260 }261 262 @Test(expected = ConstraintViolationException.class)263 public void addNewUserWhenLastNameExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {264 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);265 Mockito.doThrow(cve).when(userRepository).addUser(user);266 267 user.setLastName(this.THIRTY_ONE_CHARACTERS);268 userService.addUser(user);269 270 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));271 Mockito.verifyNoMoreInteractions(userRepository);272 }273 274 @Test(expected = ConstraintViolationException.class)275 public void addNewUserWhenUserameExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {276 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);277 Mockito.doThrow(cve).when(userRepository).addUser(user);278 279 user.setUsername(this.FIFTY_ONE_CHARACTERS);280 userService.addUser(user);281 282 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));283 Mockito.verifyNoMoreInteractions(userRepository);284 }285 286 @Test(expected = ConstraintViolationException.class)287 public void addNewUserWhenShortUsernameExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {288 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);289 Mockito.doThrow(cve).when(userRepository).addUser(user);290 291 user.setShortUsername(this.FORTY_ONE_CHARACTERS);292 userService.addUser(user);293 294 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));295 Mockito.verifyNoMoreInteractions(userRepository);296 }297 298 @Test(expected = ConstraintViolationException.class)299 public void addNewUserWhenPasswordExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {300 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);301 Mockito.doThrow(cve).when(userRepository).addUser(user);302 303 user.setPassword(this.TWO_HUNDRED_FIFTY_SIX_CHARACTERS);304 userService.addUser(user);305 306 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));307 Mockito.verifyNoMoreInteractions(userRepository);308 }309 /*310 * END TEST OF LENGTH CONSTRAINTS311 */312 313 /**314 * Add New User Tests: Test All Email Constraint Violations.315 * @throws Exception 316 * @throws ConstraintViolationException 317 */318 @Test(expected = ConstraintViolationException.class)319 public void addNewUserWhenUserameIsNotAEmailConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {320 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);321 Mockito.doThrow(cve).when(userRepository).addUser(user);322 323 user.setUsername(this.THIRTY_ONE_CHARACTERS);324 userService.addUser(user);325 326 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));327 Mockito.verifyNoMoreInteractions(userRepository);328 }329 /*330 * END TEST OF EMAIL CONSTRAINTS331 */332 333 @Test334 public void addNewUserWhenUserIsValid() throws ConstraintViolationException, Exception {335 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(null);336 Mockito.doNothing().when(userRepository).addUser(user);337 try {338 final Boolean b = userService.addUser(user);339 340 assertTrue(b);341 } catch (Exception e) {342 fail("User should add successfully");343 }344 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());345 Mockito.verify(userRepository, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));346 Mockito.verifyNoMoreInteractions(userRepository);347 }348 349 @Test350 public void addNewUserFailsWhenUserAlreadyExists() throws ConstraintViolationException, Exception { 351 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);352 try {353 final Boolean b = userService.addUser(user);354 355 assertFalse(b);356 } catch (Exception e) {357 fail("User should add successfully");358 }359 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());360 Mockito.verifyNoMoreInteractions(userRepository);361 }362 363 /**364 * Update User Tests: Should cover throwing a new Exception.365 * 366 * @throws ConstraintViolationException367 * @throws Exception368 */369 @Test(expected = Exception.class)370 public void updateUserWhenUserIsNullShouldThrowException() throws ConstraintViolationException, Exception {371 userService.updateUser(null);372 }373 374 /**375 * Update User Tests: Test All Null Constraint Violations.376 * @throws Exception 377 * @throws ConstraintViolationException 378 */379 @Test(expected = ConstraintViolationException.class)380 public void updateUserWhenFirstNameIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {381 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);382 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);383 Mockito.doThrow(cve).when(userRepository).updateUser(user);384 385 user.setFirstName(null);386 userService.updateUser(user);387 388 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());389 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));390 Mockito.verifyNoMoreInteractions(userRepository);391 }392 393 @Test(expected = ConstraintViolationException.class)394 public void updateUserWhenLastNameIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {395 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);396 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);397 Mockito.doThrow(cve).when(userRepository).updateUser(user);398 399 user.setLastName(null);400 userService.updateUser(user);401 402 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());403 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));404 Mockito.verifyNoMoreInteractions(userRepository);405 }406 407 @Test(expected = ConstraintViolationException.class)408 public void updateUserWhenUserameIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {409 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);410 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);411 Mockito.doThrow(cve).when(userRepository).updateUser(user);412 413 user.setUsername(null);414 userService.updateUser(user);415 416 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());417 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));418 Mockito.verifyNoMoreInteractions(userRepository);419 }420 421 @Test(expected = ConstraintViolationException.class)422 public void updateUserWhenShortUsernameIsNullConstraintShouldThrowConstraintViolation() throws javax.validation.ConstraintViolationException, Exception {423 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);424 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);425 Mockito.doThrow(cve).when(userRepository).updateUser(user);426 427 user.setShortUsername(null);428 userService.updateUser(user);429 430 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());431 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));432 Mockito.verifyNoMoreInteractions(userRepository);433 }434 435 @Test(expected = ConstraintViolationException.class)436 public void updateUserWhenPasswordIsNullConstraintShouldThrowConstraintViolation() throws javax.validation.ConstraintViolationException, Exception {437 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);438 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);439 Mockito.doThrow(cve).when(userRepository).updateUser(user);440 441 user.setPassword(null);442 userService.updateUser(user);443 444 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());445 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));446 Mockito.verifyNoMoreInteractions(userRepository);447 }448 /*449 * END TEST OF NULL CONSTRAINTS450 */451 452 /**453 * Update User Tests: Test All Empty Constraint Violations.454 * @throws Exception 455 * @throws ConstraintViolationException 456 */457 @Test(expected = ConstraintViolationException.class)458 public void updateUserWhenFirstNameIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {459 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);460 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);461 Mockito.doThrow(cve).when(userRepository).updateUser(user);462 463 user.setFirstName("");464 userService.updateUser(user);465 466 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());467 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));468 Mockito.verifyNoMoreInteractions(userRepository);469 }470 471 @Test(expected = ConstraintViolationException.class)472 public void updateUserWhenLastNameIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {473 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);474 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);475 Mockito.doThrow(cve).when(userRepository).updateUser(user);476 477 user.setLastName("");478 userService.updateUser(user);479 480 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());481 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));482 Mockito.verifyNoMoreInteractions(userRepository);483 }484 485 @Test(expected = ConstraintViolationException.class)486 public void updateUserWhenUserameIsEmptyConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {487 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);488 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);489 Mockito.doThrow(cve).when(userRepository).updateUser(user);490 491 user.setUsername("");492 userService.updateUser(user);493 494 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());495 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));496 Mockito.verifyNoMoreInteractions(userRepository);497 }498 499 @Test(expected = ConstraintViolationException.class)500 public void updateUserWhenShortUsernameIsEmptyConstraintShouldThrowConstraintViolation() throws javax.validation.ConstraintViolationException, Exception {501 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);502 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);503 Mockito.doThrow(cve).when(userRepository).updateUser(user);504 505 user.setShortUsername("");506 userService.updateUser(user);507 508 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());509 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));510 Mockito.verifyNoMoreInteractions(userRepository);511 }512 513 @Test(expected = ConstraintViolationException.class)514 public void updateUserWhenPasswordIsEmptyConstraintShouldThrowConstraintViolation() throws javax.validation.ConstraintViolationException, Exception {515 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);516 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);517 Mockito.doThrow(cve).when(userRepository).updateUser(user);518 519 user.setPassword("");520 userService.updateUser(user);521 522 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());523 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));524 Mockito.verifyNoMoreInteractions(userRepository);525 }526 /*527 * END TEST OF EMPTY CONSTRAINTS528 */529 530 /**531 * Update User Tests: Test All Length Constraint Violations.532 * @throws Exception 533 * @throws ConstraintViolationException 534 */535 @Test(expected = ConstraintViolationException.class)536 public void updateUserWhenFirstNameExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {537 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);538 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);539 Mockito.doThrow(cve).when(userRepository).updateUser(user);540 541 user.setFirstName(this.THIRTY_ONE_CHARACTERS);542 userService.updateUser(user);543 544 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());545 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));546 Mockito.verifyNoMoreInteractions(userRepository);547 }548 549 @Test(expected = ConstraintViolationException.class)550 public void updateUserWhenLastNameExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {551 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);552 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);553 Mockito.doThrow(cve).when(userRepository).updateUser(user);554 555 user.setLastName(this.THIRTY_ONE_CHARACTERS);556 userService.updateUser(user);557 558 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());559 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));560 Mockito.verifyNoMoreInteractions(userRepository);561 }562 563 @Test(expected = ConstraintViolationException.class)564 public void updateUserWhenUserameExceedsLengthConstraintShouldThrowConstraintViolation() throws javax.validation.ConstraintViolationException, Exception {565 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);566 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);567 Mockito.doThrow(cve).when(userRepository).updateUser(user);568 569 user.setUsername(this.FIFTY_ONE_CHARACTERS);570 userService.updateUser(user);571 572 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());573 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));574 Mockito.verifyNoMoreInteractions(userRepository);575 }576 577 @Test(expected = ConstraintViolationException.class)578 public void updateUserWhenShortUsernameExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {579 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);580 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);581 Mockito.doThrow(cve).when(userRepository).updateUser(user);582 583 user.setShortUsername(this.FORTY_ONE_CHARACTERS);584 userService.updateUser(user);585 586 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());587 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));588 Mockito.verifyNoMoreInteractions(userRepository);589 }590 591 @Test(expected = ConstraintViolationException.class)592 public void updateUserWhenPasswordExceedsLengthConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {593 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);594 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);595 Mockito.doThrow(cve).when(userRepository).updateUser(user);596 597 user.setPassword(this.TWO_HUNDRED_FIFTY_SIX_CHARACTERS);598 userService.updateUser(user);599 600 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());601 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));602 Mockito.verifyNoMoreInteractions(userRepository);603 }604 /*605 * END TEST OF LENGTH CONSTRAINTS606 */607 608 /**609 * Update User Tests: Test All Email Constraint Violations.610 * @throws Exception 611 * @throws ConstraintViolationException 612 */613 @Test(expected = ConstraintViolationException.class)614 public void updateUserWhenUserameIsNotAEmailConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {615 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);616 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);617 Mockito.doThrow(cve).when(userRepository).updateUser(user);618 619 user.setUsername(this.THIRTY_ONE_CHARACTERS);620 userService.updateUser(user);621 622 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());623 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));624 Mockito.verifyNoMoreInteractions(userRepository);625 }626 /*627 * END TEST OF EMAIL CONSTRAINTS628 */629 630 /**631 * Update User Tests: Add New User is successful.632 * @throws ConstraintViolationException 633 * @throws Exception 634 */635 636 @Test637 public void updateUserWhenUserIsValid() throws ConstraintViolationException, Exception {638 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);639 Mockito.doNothing().when(userRepository).updateUser(user);640 try {641 final Boolean b = userService.updateUser(user);642 643 assertTrue(b);644 } catch (Exception e) {645 fail("User should add successfully");646 }647 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());648 Mockito.verify(userRepository, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));649 Mockito.verifyNoMoreInteractions(userRepository);650 }651 652 @Test653 public void updateUserFailsWhenUserDoesntExists() throws ConstraintViolationException, Exception { 654 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(null);655 try {656 final Boolean b = userService.updateUser(user);657 658 assertFalse(b);659 } catch (Exception e) {660 fail("User should add successfully");661 }662 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());663 Mockito.verifyNoMoreInteractions(userRepository);664 }665 666 /**667 * Delete User Tests: Should cover throwing a new Exception.668 * 669 * @throws Exception670 */671 @Test(expected = Exception.class)672 public void deleteUserWhenUserIsNullShouldThrowException() throws Exception {673 userService.deleteUser(null);674 }675 676 @Test677 public void deleteUserWhenUserIsValid() throws Exception {678 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);679 Mockito.doNothing().when(userRepository).deleteUser(user);680 Mockito.when(userRepository.findById(Mockito.anyLong())).thenReturn(null);681 try {682 final Boolean b = userService.deleteUser(user);683 684 assertTrue(b);685 } catch (Exception e) {686 fail("User should add successfully");687 }688 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());689 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());690 Mockito.verify(userRepository, VerificationModeFactory.times(1)).deleteUser(Mockito.any(User.class));691 Mockito.verifyNoMoreInteractions(userRepository);692 }693 694 @Test695 public void deleteUserWhenUserDoesNotExist() throws Exception {696 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(null);697 try {698 final Boolean b = userService.deleteUser(user);699 700 assertFalse(b);701 } catch (Exception e) {702 fail("User should add successfully");703 }704 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());705 Mockito.verifyNoMoreInteractions(userRepository);706 }707 708 @Test709 public void deleteUserWhenUserExistsButFailsToActuallyDelete() throws Exception {710 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);711 Mockito.doNothing().when(userRepository).deleteUser(user);712 Mockito.when(userRepository.findById(Mockito.anyLong())).thenReturn(user);713 try {714 final Boolean b = userService.deleteUser(user);715 716 assertFalse(b);717 } catch (Exception e) {718 fail("User should add successfully");719 }720 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());721 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());722 Mockito.verify(userRepository, VerificationModeFactory.times(1)).deleteUser(Mockito.any(User.class));723 Mockito.verifyNoMoreInteractions(userRepository);724 }725 726 @Test(expected = RuntimeException.class)727 public void findUserByIdThrowsRuntimeException() throws HibernateException, RuntimeException {728 Mockito.doThrow(new RuntimeException()).when(userRepository).findById(Mockito.anyLong());729 730 userService.findById(1L);731 732 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());733 Mockito.verifyNoMoreInteractions(userRepository);734 }735 736 @Test(expected = HibernateException.class)737 public void findUserByIdThrowsHibernateException() throws HibernateException, RuntimeException {738 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(userRepository).findById(Mockito.anyLong());739 740 userService.findById(1L);741 742 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());743 Mockito.verifyNoMoreInteractions(userRepository);744 }745 746 @Test747 public void findUserByIdIsSuccessful() {748 user.setId(1L);749 Mockito.when(userRepository.findById(Mockito.anyLong())).thenReturn(user);750 751 try {752 User u = userService.findById(1L);753 754 assertNotNull(u);755 } catch (Exception e) {756 fail("No Exceptions should be thrown: user should not be null.");757 }758 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());759 Mockito.verifyNoMoreInteractions(userRepository);760 }761 762 @Test763 public void findUserByIdFails(){764 Mockito.when(userRepository.findById(Mockito.anyLong())).thenReturn(null);765 766 try {767 User u = userService.findById(1L);768 769 assertNull(u);770 } catch (Exception e) {771 fail("No Exceptions should be thrown: user should be null.");772 }773 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());774 Mockito.verifyNoMoreInteractions(userRepository);775 }776 777 @Test(expected = RuntimeException.class)778 public void findUserByUsernameThrowsRuntimeException() throws RuntimeException {779 Mockito.doThrow(new RuntimeException()).when(userRepository).findByUsername(Mockito.anyString());780 781 userService.findByUsername("test@fake.com");782 783 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());784 Mockito.verifyNoMoreInteractions(userRepository);785 }786 787 @Test(expected = HibernateException.class)788 public void findUserByUsernameThrowsHibernateException() throws HibernateException {789 List<User> lst = new ArrayList<User>();790 lst.add(user);791 792 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(userRepository).findByUsername(Mockito.anyString());793 794 userService.findByUsername("test@fake.com");795 796 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());797 Mockito.verifyNoMoreInteractions(userRepository);798 }799 800 @Test801 public void findUserByUsernameIsSuccessful() {802 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(user);803 804 try {805 User u = userService.findByUsername("test@fake.com");806 807 assertNotNull(u);808 } catch (Exception e) {809 fail("No Exceptions should be thrown: user should not be null.");810 }811 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());812 Mockito.verifyNoMoreInteractions(userRepository);813 }814 815 @Test816 public void findUserByUsernameFailsWhenReturnsNull(){817 Mockito.when(userRepository.findByUsername(Mockito.anyString())).thenReturn(null);818 819 try {820 User u = userService.findByUsername("test@fake.com");821 822 assertNull(u);823 } catch (Exception e) {824 fail("No Exceptions should be thrown: user should be null.");825 }826 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findByUsername(Mockito.anyString());827 Mockito.verifyNoMoreInteractions(userRepository);828 }829 830 @Test(expected = RuntimeException.class)831 public void findUsersByRoleThrowsRuntimeException() throws RuntimeException {832 Mockito.doThrow(new RuntimeException()).when(userRepository).findUsersByRole(Mockito.anyLong());833 834 userService.findUsersByRole(1L);835 836 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findUsersByRole(Mockito.anyLong());837 Mockito.verifyNoMoreInteractions(userRepository);838 }839 840 @Test(expected = HibernateException.class)841 public void findUsersByRoleThrowsHibernateException() throws HibernateException {842 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(userRepository).findUsersByRole(Mockito.anyLong());843 844 userService.findUsersByRole(1L);845 846 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findUsersByRole(Mockito.anyLong());847 Mockito.verifyNoMoreInteractions(userRepository);848 }849 850 @Test851 public void findUsersByRoleIsSuccessful() {852 List<User> lst = new ArrayList<User>();853 lst.add(user);854 855 Mockito.when(userRepository.findUsersByRole(Mockito.anyLong())).thenReturn(lst);856 857 try {858 List<User> u = userService.findUsersByRole(1L);859 860 assertNotNull(u);861 } catch (Exception e) {862 fail("No Exceptions should be thrown: user should not be null.");863 }864 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findUsersByRole(Mockito.anyLong());865 Mockito.verifyNoMoreInteractions(userRepository);866 }867 868 @Test869 public void findUsersByRoleFails(){870 Mockito.when(userRepository.findUsersByRole(Mockito.anyLong())).thenReturn(null);871 872 try {873 List<User> u = userService.findUsersByRole(1L);874 875 assertNull(u);876 } catch (Exception e) {877 fail("No Exceptions should be thrown: user should be null.");878 }879 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findUsersByRole(Mockito.anyLong());880 Mockito.verifyNoMoreInteractions(userRepository);881 }882 883 @Test(expected = RuntimeException.class)884 public void findAllUsersThrowsRuntimeException() throws RuntimeException {885 Mockito.doThrow(new RuntimeException()).when(userRepository).findAllUsers();886 887 userService.findAllUsers();888 889 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findAllUsers();890 Mockito.verifyNoMoreInteractions(userRepository);891 }892 893 @Test(expected = HibernateException.class)894 public void findAllUsersThrowsHibernateException() throws HibernateException {895 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(userRepository).findAllUsers();896 897 userService.findAllUsers();898 899 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findAllUsers();900 Mockito.verifyNoMoreInteractions(userRepository);901 }902 903 @Test904 public void findAllUsersIsSuccessful() {905 List<User> lst = new ArrayList<User>();906 lst.add(user);907 908 Mockito.when(userRepository.findAllUsers()).thenReturn(lst);909 ...

Full Screen

Full Screen

Source:AssetFileGeneratorTest.java Github

copy

Full Screen

...258 @Test259 public void generateFilesTest_Exception() throws Exception{260 261 mockStatic(FileManager.class);262 PowerMockito.doThrow(new IOException()).when(FileManager.class,"initialise",anyString());263 264 ReflectionTestUtils.setField(assetFileGenerator, "targetTypes", "ec2,asg,stack,dynamodb,efs,emr,lambda,classicelb,appelb,targetgroup,"265 + "nat,rdsdb,rdscluster,s3,eni,sg,subnet,checks,redshift,volume,snapshot,vpc,api,iamuser,rdssnapshot,iamrole,kms,cloudfront,beanstalk,phd,"266 + "routetable,networkacl,elasticip,launchconfig,internetgw,vpngw,asgpolicy,snstopic,egressgateway,dhcpoption,peeringconnection,customergateway,"267 + "vpnconnection,directconnect,virtualinterface,elasticsearch,reserved instances,ssm");268 when(credProvider.getCredentials(anyString(), anyString())).thenReturn(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"));269 270 mockStatic(InventoryUtil.class);271 mockStatic(EC2InventoryUtil.class);272 mockStatic(ASGInventoryUtil.class);273 mockStatic(DirectConnectionInventoryUtil.class);274 mockStatic(ESInventoryUtil.class);275 mockStatic(SNSInventoryUtil.class);276 mockStatic(ErrorManageUtil.class);277 278 PowerMockito.doNothing().when(ErrorManageUtil.class,"uploadError",anyString(),anyString(),anyString(),anyString());279 280 when(InventoryUtil.fetchInstances(anyObject(), anyString(), anyString(), anyString(),anyString())).thenReturn(new HashMap<>());281 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateInstanceFiles",new HashMap<>());282 283 when(InventoryUtil.fetchAsg(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());284 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateAsgFiles",new HashMap<>());285 286 when(InventoryUtil.fetchCloudFormationStack(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());287 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateCloudFormationStackFiles",new HashMap<>());288 289 when(InventoryUtil.fetchDynamoDBTables(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());290 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDynamoDbFiles",new HashMap<>());291 292 when(InventoryUtil.fetchEFSInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());293 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEfsFiles",new HashMap<>());294 295 when(InventoryUtil.fetchEMRInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());296 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEmrFiles",new HashMap<>());297 298 when(InventoryUtil.fetchLambdaInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());299 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateLamdaFiles",new HashMap<>());300 301 when(InventoryUtil.fetchClassicElbInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());302 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateClassicElbFiles",new HashMap<>());303 304 when(InventoryUtil.fetchElbInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());305 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateApplicationElbFiles",new HashMap<>());306 307 when(InventoryUtil.fetchTargetGroups(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());308 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateTargetGroupFiles",new HashMap<>());309 310 when(InventoryUtil.fetchNATGatewayInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());311 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateNatGatewayFiles",new HashMap<>());312 313 when(InventoryUtil.fetchRDSInstanceInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());314 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRDSInstanceFiles",new HashMap<>());315 316 when(InventoryUtil.fetchRDSClusterInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());317 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRDSClusterFiles",new HashMap<>());318 319 when(InventoryUtil.fetchS3Info(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap());320 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateS3Files",new HashMap<>());321 322 when(InventoryUtil.fetchNetworkIntefaces(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());323 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateNwInterfaceFiles",new HashMap<>());324 325 when(InventoryUtil.fetchSecurityGroups(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());326 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSecGroupFile",new HashMap<>());327 328 when(InventoryUtil.fetchSubnets(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());329 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSubnetFiles",new HashMap<>());330 331 when(InventoryUtil.fetchTrusterdAdvisorsChecks(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());332 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateTrustedAdvisorFiles",new HashMap<>());333 334 when(InventoryUtil.fetchRedshiftInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());335 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRedshiftFiles",new HashMap<>());336 337 when(InventoryUtil.fetchVolumetInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());338 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generatefetchVolumeFiles",new HashMap<>());339 340 when(InventoryUtil.fetchSnapshots(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());341 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSnapshotFiles",new HashMap<>());342 343 when(InventoryUtil.fetchVpcInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());344 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateVpcFiles",new HashMap<>());345 346 when(InventoryUtil.fetchApiGateways(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());347 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateApiGatewayFiles",new HashMap<>());348 349 when(InventoryUtil.fetchIAMUsers(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());350 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateIamUserFiles",new HashMap<>());351 352 when(InventoryUtil.fetchRDSDBSnapshots(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());353 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRDSSnapshotFiles",new HashMap<>());354 355 when(InventoryUtil.fetchIAMRoles(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());356 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateIamRoleFiles",new HashMap<>());357 358 when(InventoryUtil.fetchKMSKeys(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());359 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateKMSFiles",new HashMap<>());360 361 when(InventoryUtil.fetchCloudFrontInfo(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());362 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateCloudFrontFiles",new HashMap<>());363 364 when(InventoryUtil.fetchEBSInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());365 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEBSFiles",new HashMap<>());366 367 when(InventoryUtil.fetchPHDInfo(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());368 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generatePHDFiles",new HashMap<>());369 370 when(EC2InventoryUtil.fetchRouteTables(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());371 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEC2RouteTableFiles",new HashMap<>());372 373 when(EC2InventoryUtil.fetchNetworkACL(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());374 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateNetworkAclFiles",new HashMap<>());375 376 when(EC2InventoryUtil.fetchElasticIPAddresses(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());377 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateElasticIPFiles",new HashMap<>());378 379 when(ASGInventoryUtil.fetchLaunchConfigurations(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());380 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateLaunchConfigurationsFiles",new HashMap<>());381 382 when(EC2InventoryUtil.fetchInternetGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());383 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateInternetGatewayFiles",new HashMap<>());384 385 when(EC2InventoryUtil.fetchVPNGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());386 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateVPNGatewayFiles",new HashMap<>());387 388 when(ASGInventoryUtil.fetchScalingPolicies(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());389 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateScalingPolicies",new HashMap<>());390 391 when(SNSInventoryUtil.fetchSNSTopics(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());392 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSNSTopics",new HashMap<>());393 394 when(EC2InventoryUtil.fetchEgressGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());395 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEgressGateway",new HashMap<>());396 397 when(EC2InventoryUtil.fetchDHCPOptions(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());398 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDhcpOptions",new HashMap<>());399 400 when(EC2InventoryUtil.fetchPeeringConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());401 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generatePeeringConnections",new HashMap<>());402 403 when(EC2InventoryUtil.fetchCustomerGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());404 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateCustomerGateway",new HashMap<>());405 406 when(EC2InventoryUtil.fetchVPNConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());407 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateVpnConnection",new HashMap<>());408 409 when(DirectConnectionInventoryUtil.fetchDirectConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());410 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDirectConnection",new HashMap<>());411 412 when(DirectConnectionInventoryUtil.fetchDirectConnectionsVirtualInterfaces(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());413 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDirectConnectionVirtualInterfaces",new HashMap<>());414 415 when(ESInventoryUtil.fetchESInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());416 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateESDomain",new HashMap<>());417 418 when(EC2InventoryUtil.fetchReservedInstances(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());419 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateReservedInstances",new HashMap<>());420 421 when(EC2InventoryUtil.fetchSSMInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());422 PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSsmFiles",new HashMap<>());423 424 List<Map<String,String>> accounts = new ArrayList<>();425 Map<String,String> account = new HashMap<>();426 account.put(InventoryConstants.ACCOUNT_ID, "account");427 account.put(InventoryConstants.ACCOUNT_NAME, "accountName");428 accounts.add(account);429 assetFileGenerator.generateFiles(accounts , "skipRegions", "filePath");430 431 }432}...

Full Screen

Full Screen

Source:PowerMockitoStubber.java Github

copy

Full Screen

...26public interface PowerMockitoStubber extends Stubber {27 28 /**29 * Allows to choose a static method when stubbing in30 * doThrow()|doAnswer()|doNothing()|doReturn() style31 * <p>32 * Example:33 * </p>34 * <pre>35 * doThrow(new RuntimeException()).when(StaticList.class);36 * StaticList.clear();37 *38 * //following throws RuntimeException:39 * StaticList.clear();40 * </pre>41 * <p>42 * Read more about those methods:43 * </p>44 * <p>45 * {@link Mockito#doThrow(Class)}46 * </p>47 * <p>48 * {@link Mockito#doAnswer(Answer)}49 * </p>50 * <p>51 * {@link Mockito#doNothing()}52 * </p>53 * <p>54 * {@link Mockito#doReturn(Object)}55 * </p>56 *57 * @param classMock the mock class58 * @see Mockito59 */60 void when(Class<?> classMock);61 62 /**63 * Allows to mock a private instance method when stubbing in64 * doThrow()|doAnswer()|doNothing()|doReturn() style.65 * <p>66 * Example:67 * <pre>68 * doThrow(new RuntimeException()).when(instance, method(&quot;myMethod&quot;)).withNoArguments();69 * </pre>70 * </p>71 * <p>72 * Read more about those methods:73 * </p>74 * <p>75 * {@link Mockito#doThrow(Class)}76 * </p>77 * <p>78 * {@link Mockito#doAnswer(Answer)}79 * </p>80 * <p>81 * {@link Mockito#doNothing()}82 * </p>83 * <p>84 * {@link Mockito#doReturn(Object)}85 * </p>86 *87 * @param mock the method88 * @param method private method to be mocked89 * @see Mockito90 */91 <T> PrivatelyExpectedArguments when(T mock, Method method) throws Exception;92 93 /**94 * Allows to mock a private instance method based on the parameters when95 * stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style.96 * <p>97 * Example:98 * </p>99 * <p>100 * <pre>101 * doThrow(new RuntimeException()).when(instance, parameter1, parameter2);102 * </pre>103 * </p>104 * <p>105 * Read more about those methods:106 * </p>107 * <p>108 * {@link Mockito#doThrow(Throwable...)}109 * </p>110 * <p>111 * {@link Mockito#doAnswer(Answer)}112 * </p>113 * <p>114 * {@link Mockito#doNothing()}115 * </p>116 * <p>117 * {@link Mockito#doReturn(Object)}118 * </p>119 *120 * @param mock the Mock121 * @param arguments array of arguments is used to find suitable method to be mocked.122 * @see Mockito123 */124 <T> void when(T mock, Object... arguments) throws Exception;125 126 /**127 * Allows to mock a private instance method based on method name and128 * parameters when stubbing in doThrow()|doAnswer()|doNothing()|doReturn()129 * style.130 * <p>131 * Example:132 * </p>133 * <pre>134 * doThrow(new RuntimeException()).when(instance, &quot;methodName&quot;, parameter1, parameter2);135 * </pre>136 * </p>137 * <p>138 * Read more about those methods:139 * </p>140 * <p>141 * {@link Mockito#doThrow(Throwable...)}142 * </p>143 * <p>144 * {@link Mockito#doAnswer(Answer)}145 * </p>146 * <p>147 * {@link Mockito#doNothing()}148 * </p>149 * <p>150 * {@link Mockito#doReturn(Object)}151 * </p>152 *153 * @param mock the Mock154 * @param methodToExpect name of method which have to mocked155 * @param arguments array of arguments of <code>methodToExpect</code>156 * @see Mockito157 */158 <T> void when(T mock, String methodToExpect, Object... arguments) throws Exception;159 160 /**161 * Allows to mock a static private method when stubbing in162 * doThrow()|doAnswer()|doNothing()|doReturn() style.163 * <p>164 * Example:165 * <pre>166 * doThrow(new RuntimeException()).when(MyClass.class, method(&quot;myMethod&quot;)).withNoArguments();167 * </pre>168 * </p>169 * <p>170 * Read more about those methods:171 * </p>172 * <p>173 * {@link Mockito#doThrow(Throwable...)}174 * </p>175 * <p>176 * {@link Mockito#doAnswer(Answer)}177 * </p>178 * <p>179 * {@link Mockito#doNothing()}180 * </p>181 * <p>182 * {@link Mockito#doReturn(Object)}183 * </p>184 *185 * @param classMock class owner of private static method186 * @param method private static method to be mocked187 * @see Mockito188 */189 <T> PrivatelyExpectedArguments when(Class<T> classMock, Method method) throws Exception;190 191 /**192 * Allows to mock a static private method based on the parameters when193 * stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style.194 * <p>195 * Example:196 * <pre>197 * doThrow(new RuntimeException()).when(MyClass.class, parameter1, parameter2);198 * </pre>199 * </p>200 * <p>201 * Read more about those methods:202 * </p>203 * <p>204 * {@link Mockito#doThrow(Throwable...)}205 * </p>206 * <p>207 * {@link Mockito#doAnswer(Answer)}208 * </p>209 * <p>210 * {@link Mockito#doNothing()}211 * </p>212 * <p>213 * {@link Mockito#doReturn(Object)}214 * </p>215 *216 * @param classMock class owner of private static method217 * @param arguments array of arguments is used to find suitable method to be mocked.218 * @see Mockito219 */220 <T> void when(Class<T> classMock, Object... arguments) throws Exception;221 222 /**223 * Allows to mock a static private method based on method name and224 * parameters when stubbing in doThrow()|doAnswer()|doNothing()|doReturn()225 * style.226 * <p>227 * Example:228 * <pre>229 * doThrow(new RuntimeException()).when(MyClass.class, &quot;methodName&quot;, parameter1, parameter2);230 * </pre>231 * </p>232 * <p>233 * Read more about those methods:234 * </p>235 * <p>236 * {@link Mockito#doThrow(Throwable...)}237 * </p>238 * <p>239 * {@link Mockito#doAnswer(Answer)}240 * </p>241 * <p>242 * {@link Mockito#doNothing()}243 * </p>244 * <p>245 * {@link Mockito#doReturn(Object)}246 * </p>247 *248 * @param classMock the class owner of static private method249 * @param methodToExpect name of method which have to mocked250 * @param arguments array of arguments of <code>methodToExpect</code>...

Full Screen

Full Screen

Source:BaseStubber.java Github

copy

Full Screen

...13 */14@NotExtensible15public interface BaseStubber {16 /**17 * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Throwable[])} style:18 * <pre class="code"><code class="java">19 * doThrow(new RuntimeException("one")).20 * doThrow(new RuntimeException("two"))21 * .when(mock).someVoidMethod();22 * </code></pre>23 * See javadoc for {@link Mockito#doThrow(Throwable[])}24 *25 * @param toBeThrown to be thrown when the stubbed method is called26 * @return stubber - to select a method for stubbing27 */28 Stubber doThrow(Throwable... toBeThrown);29 /**30 * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Class)} style:31 * <pre class="code"><code class="java">32 * doThrow(RuntimeException.class).33 * doThrow(IllegalArgumentException.class)34 * .when(mock).someVoidMethod();35 * </code></pre>36 * See javadoc for {@link Mockito#doThrow(Class)}37 *38 * @param toBeThrown exception class to be thrown when the stubbed method is called39 * @return stubber - to select a method for stubbing40 *41 * @since 2.1.042 */43 Stubber doThrow(Class<? extends Throwable> toBeThrown);44 /**45 * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Class)} style:46 * <pre class="code"><code class="java">47 * doThrow(RuntimeException.class).48 * doThrow(IllegalArgumentException.class)49 * .when(mock).someVoidMethod();50 * </code></pre>51 * See javadoc for {@link Mockito#doThrow(Class)}52 *53 * @param toBeThrown exception class to be thrown when the stubbed method is called54 * @param nextToBeThrown exception class next to be thrown when the stubbed method is called55 * @return stubber - to select a method for stubbing56 *57 * @since 2.1.058 */59 // Additional method helps users of JDK7+ to hide heap pollution / unchecked generics array60 // creation61 @SuppressWarnings({"unchecked", "varargs"})62 Stubber doThrow(63 Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown);64 /**65 * Use it for stubbing consecutive calls in {@link Mockito#doAnswer(Answer)} style:66 * <pre class="code"><code class="java">67 * doAnswer(answerOne).68 * doAnswer(answerTwo)69 * .when(mock).someVoidMethod();70 * </code></pre>71 * See javadoc for {@link Mockito#doAnswer(Answer)}72 *73 * @param answer to answer when the stubbed method is called74 * @return stubber - to select a method for stubbing75 */76 Stubber doAnswer(Answer answer);77 /**78 * Use it for stubbing consecutive calls in {@link Mockito#doNothing()} style:79 * <pre class="code"><code class="java">80 * doNothing().81 * doThrow(new RuntimeException("two"))82 * .when(mock).someVoidMethod();83 * </code></pre>84 * See javadoc for {@link Mockito#doNothing()}85 *86 * @return stubber - to select a method for stubbing87 */88 Stubber doNothing();89 /**90 * Use it for stubbing consecutive calls in {@link Mockito#doReturn(Object)} style.91 * <p>92 * See javadoc for {@link Mockito#doReturn(Object)}93 *94 * @param toBeReturned to be returned when the stubbed method is called95 * @return stubber - to select a method for stubbing...

Full Screen

Full Screen

Source:TimeoutTest.java Github

copy

Full Screen

...30 @Test31 public void should_fail_because_verification_fails() {32 Timeout t = new Timeout(1, mode, timer);33 when(timer.isCounting()).thenReturn(true, true, true, false);34 doThrow(error).doThrow(error).doThrow(error).when(mode).verify(data);35 try {36 t.verify(data);37 fail();38 } catch (MockitoAssertionError e) {39 }40 verify(timer, times(4)).isCounting();41 }42 @Test43 public void should_pass_even_if_first_verification_fails() {44 Timeout t = new Timeout(1, mode, timer);45 when(timer.isCounting()).thenReturn(true, true, true, false);46 doThrow(error).doThrow(error).doNothing().when(mode).verify(data);47 t.verify(data);48 verify(timer, times(3)).isCounting();49 }50 @Test51 public void should_try_to_verify_correct_number_of_times() {52 Timeout t = new Timeout(10, mode, timer);53 doThrow(error).when(mode).verify(data);54 when(timer.isCounting()).thenReturn(true, true, true, true, true, false);55 try {56 t.verify(data);57 fail();58 } catch (MockitoAssertionError e) {59 }60 verify(mode, times(5)).verify(data);61 }62}...

Full Screen

Full Screen

Source:DefaultLenientStubber.java Github

copy

Full Screen

...11import org.mockito.stubbing.Stubber;12public class DefaultLenientStubber implements LenientStubber {13 private static final MockitoCore MOCKITO_CORE = new MockitoCore();14 @Override15 public Stubber doThrow(Throwable... toBeThrown) {16 return stubber().doThrow(toBeThrown);17 }18 @Override19 public Stubber doThrow(Class<? extends Throwable> toBeThrown) {20 return stubber().doThrow(toBeThrown);21 }22 @Override23 public Stubber doThrow(24 Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) {25 return stubber().doThrow(toBeThrown, nextToBeThrown);26 }27 @Override28 public Stubber doAnswer(Answer answer) {29 return stubber().doAnswer(answer);30 }31 @Override32 public Stubber doNothing() {33 return stubber().doNothing();34 }35 @Override36 public Stubber doReturn(Object toBeReturned) {37 return stubber().doReturn(toBeReturned);38 }39 @Override...

Full Screen

Full Screen

Source:Stubber.java Github

copy

Full Screen

...6import org.mockito.CheckReturnValue;7import org.mockito.Mockito;8import org.mockito.NotExtensible;9/**10 * Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style11 * <p>12 * Example:13 * <pre class="code"><code class="java">14 * doThrow(new RuntimeException()).when(mockedList).clear();15 *16 * //following throws RuntimeException:17 * mockedList.clear();18 * </code></pre>19 *20 * Also useful when stubbing consecutive calls:21 *22 * <pre class="code"><code class="java">23 * doThrow(new RuntimeException("one")).24 * doThrow(new RuntimeException("two"))25 * .when(mock).someVoidMethod();26 * </code></pre>27 *28 * Read more about those methods:29 * <p>30 * {@link Mockito#doThrow(Throwable[])}31 * <p>32 * {@link Mockito#doAnswer(Answer)}33 * <p>34 * {@link Mockito#doNothing()}35 * <p>36 * {@link Mockito#doReturn(Object)}37 * <p>38 *39 * See examples in javadoc for {@link Mockito}40 */41@CheckReturnValue42@SuppressWarnings("unchecked")43@NotExtensible44public interface Stubber extends BaseStubber {45 /**46 * Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style47 * <p>48 * Example:49 * <pre class="code"><code class="java">50 * doThrow(new RuntimeException())51 * .when(mockedList).clear();52 *53 * //following throws RuntimeException:54 * mockedList.clear();55 * </code></pre>56 *57 * Read more about those methods:58 * <p>59 * {@link Mockito#doThrow(Throwable[])}60 * <p>61 * {@link Mockito#doAnswer(Answer)}62 * <p>63 * {@link Mockito#doNothing()}64 * <p>65 * {@link Mockito#doReturn(Object)}66 * <p>67 *68 * See examples in javadoc for {@link Mockito}69 *70 * @param mock The mock71 * @return select method for stubbing72 */73 <T> T when(T mock);...

Full Screen

Full Screen

doThrow

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2import org.mockito.stubbing.OngoingStubbing;3import org.mockito.invocation.InvocationOnMock;4import org.mockito.stubbing.Answer;5import static org.mockito.Mockito.doThrow;6import static org.mockito.Mockito.mock;7import static org.mockito.Mockito.verify;8import java.io.IOException;9public class 1 {10 public static void main(String[] args) throws IOException {11 Class1 mockObj = mock(Class1.class);12 Class1 obj = new Class1();13 doThrow(new IOException()).when(mockObj).method1();14 mockObj.method1();15 verify(mockObj).method1();16 }17}18 at org.mockito.internal.stubbing.answers.ThrowsException.answer(ThrowsException.java:16)19 at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:91)20 at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)21 at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)22 at org.mockito.internal.creation.cglib.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:59)23 at Class1$$EnhancerByMockitoWithCGLIB$$e8c8b6d0.method1()24 at 1.main(1.java:18)25Mockito when() Method

Full Screen

Full Screen

doThrow

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2import org.mockito.exceptions.base.MockitoException;3import org.mockito.invocation.InvocationOnMock;4import org.mockito.stubbing.Answer;5import org.mockito.stubbing.OngoingStubbing;6public class MockitoExample {7 public static void main(String[] args) {8 Person person = Mockito.mock(Person.class);9 doThrow(new RuntimeException()).when(person).setName(Mockito.anyString());10 person.setName("ab");11 }12}13 at MockitoExample.main(MockitoExample.java:19)14Related posts: Mockito doThrow() method example Mockito doAnswer() method example Mockito doReturn() method example Mockito doNothing() method example Mockito doCallRealMethod() method example Mockito when() method example Mockito spy() method example Mockito mock() method example Mockito verify() method example Mockito verifyNoMoreInteractions() method example Mockito verifyNoInteractions() method example Mockito veri

Full Screen

Full Screen

doThrow

Using AI Code Generation

copy

Full Screen

1package com.ack.util;2import java.util.List;3import org.junit.Test;4import org.mockito.Mockito;5import static org.mockito.Mockito.doThrow;6public class DoThrowTest {7 public void testDoThrow() {8 List mockList = Mockito.mock( List.class );9 doThrow( new RuntimeException() ).when( mockList ).clear();10 mockList.clear();11 }12}

Full Screen

Full Screen

doThrow

Using AI Code Generation

copy

Full Screen

1package com.ack.junit.mock;2import static org.mockito.Mockito.doThrow;3import java.util.List;4import org.junit.Test;5public class DoThrowTest {6 public void testDoThrow() {7 List mockList = mock( List.class );8 doThrow( new RuntimeException() ).when( mockList ).clear();9 mockList.clear();10 }11}12package com.ack.junit.mock;13import static org.mockito.Mockito.doThrow;14import java.util.List;15import org.junit.Test;16public class DoThrowTest {17 public void testDoThrow() {18 List mockList = mock( List.class );19 doThrow( new RuntimeException() ).when( mockList ).clear();20 mockList.clear();21 }22}23package com.ack.junit.mock;24import static org.mockito.Mockito.doThrow;25import java.util.List;26import org.junit.Test;27public class DoThrowTest {28 public void testDoThrow() {29 List mockList = mock( List.class );30 doThrow( new RuntimeException() ).when( mockList ).clear();31 mockList.clear();32 }33}34package com.ack.junit.mock;35import static org.mockito.Mockito.doThrow;36import java.util.List;37import org.junit.Test;38public class DoThrowTest {39 public void testDoThrow() {40 List mockList = mock( List.class );41 doThrow( new RuntimeException() ).when( mockList ).clear();42 mockList.clear();43 }44}45package com.ack.junit.mock;46import static org.mockito.Mockito.doThrow;47import java.util.List;48import org.junit.Test;49public class DoThrowTest {

Full Screen

Full Screen

doThrow

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2import org.mockito.stubbing.OngoingStubbing;3public class 1 {4 public static void main(String[] args) {5 List mockedList = Mockito.mock(List.class);6 Exception exception = new Exception("Exception");7 OngoingStubbing ongoingStubbing = Mockito.doThrow(exception).when(mockedList).add("one");8 try {9 mockedList.add("one");10 } catch (Exception e) {11 System.out.println(e);12 }13 }14}15Related Posts: Mockito - doThrow() method16Mockito - doAnswer() method17Mockito - doCallRealMethod() method18Mockito - doNothing() method19Mockito - doReturn() method20Mockito - doAnswer() method21Mockito - doCallRealMethod() method22Mockito - doNothing() method

Full Screen

Full Screen

doThrow

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2import org.mockito.stubbing.OngoingStubbing;3import org.mockito.stubbing.Stubber;4import java.io.IOException;5public class 1 {6 public static void main(String[] args) throws IOException {7 String mock = Mockito.mock(String.class);8 Stubber stub = Mockito.doThrow(new IOException());9 OngoingStubbing<String> stubb = stub.when(mock);10 stubb.toString();11 }12}13Cannot stub the following methods: class java.lang.String.toString()14at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:36)15at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)16at org.mockito.Mockito.mock(Mockito.java:1916)17at org.mockito.Mockito.mock(Mockito.java:1825)18at 1.main(1.java:14)

Full Screen

Full Screen

doThrow

Using AI Code Generation

copy

Full Screen

1package com.ack.test.mocking;2import java.util.List;3import org.junit.Test;4import static org.mockito.Mockito.*;5public class MockitoDoThrowTest {6 public void testDoThrow() {7 List mockedList = mock( List.class );8 doThrow( new RuntimeException() ).when( mockedList ).clear();9 mockedList.clear();10 }11}12package com.ack.test.mocking;13import java.util.List;14import org.junit.Test;15import static org.mockito.Mockito.*;16public class MockitoDoAnswerTest {17 public void testDoAnswer() {18 List mockedList = mock( List.class );19 doAnswer( invocation -> {20 Object[] args = invocation.getArguments();21 Object mock = invocation.getMock();22 return "called with arguments: " + args;23 } ).when( mockedList ).get( 0 );24 System.out.println( mockedList.get( 0 ) );25 }26}27package com.ack.test.mocking;28import java.util.List;29import org.junit.Test;30import static org.mockito.Mockito.*;31public class MockitoDoReturnTest {32 public void testDoReturn() {33 List mockedList = mock( List.class );34 doReturn( "foo" ).when( mockedList ).get( 0 );35 System.out.println( mockedList.get( 0 ) );36 }37}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful