Best Mockito code snippet using org.mockito.Mockito.verifyNoMoreInteractions
Source:UserServiceTest.java
...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 910 try {911 List<User> u = userService.findAllUsers();912 913 assertNotNull(u);914 } catch (Exception e) {915 fail("No Exceptions should be thrown: user should not be null.");916 }917 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findAllUsers();918 Mockito.verifyNoMoreInteractions(userRepository);919 }920 921 @Test922 public void findAllUsersFails(){923 Mockito.when(userRepository.findAllUsers()).thenReturn(null);924 925 try {926 List<User> u = userService.findAllUsers();927 928 assertNull(u);929 } catch (Exception e) {930 fail("No Exceptions should be thrown: user should be null.");931 }932 Mockito.verify(userRepository, VerificationModeFactory.times(1)).findAllUsers();933 Mockito.verifyNoMoreInteractions(userRepository);934 } 935}
...
Source:RoleControllerTest.java
...66 67 assertEquals(view, "role/add-role");68 69 Mockito.verify(model).addAttribute(Mockito.anyString(), Mockito.any(Role.class));70 Mockito.verifyNoMoreInteractions(model);71 }72 73 @Test 74 public void addingRoleReturnValueWhenResultHasError() {75 Mockito.when(result.hasErrors()).thenReturn(true);76 77 final String view = roleController.addingRole(role, result, model);78 79 assertEquals(view, "role/add-role");80 81 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();82 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(Role.class));83 Mockito.verify(model).addAttribute("errorMessage", "error.have.occured");84 Mockito.verifyNoMoreInteractions(result);85 Mockito.verifyNoMoreInteractions(model);86 }87 88 @Test 89 public void addingRoleReturnValueWhenResultHasNoErrorsAndThrowsConstraintViolation() throws ConstraintViolationException, Exception {90 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);91 Mockito.when(result.hasErrors()).thenReturn(false);92 Mockito.doThrow(cve).when(roleService).addRole(Mockito.any(Role.class));9394 final String view = roleController.addingRole(role, result, model);95 assertEquals(view, "admin/admin");96 97 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();98 Mockito.verify(roleService, VerificationModeFactory.times(1)).addRole(Mockito.any(Role.class));99 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", cve.getMessage());100 Mockito.verifyNoMoreInteractions(result);101 Mockito.verifyNoMoreInteractions(roleService);102 Mockito.verifyNoMoreInteractions(model);103 }104 105 @Test 106 public void addingRoleReturnValueWhenResultHasNoErrorsAndThrowsException() throws ConstraintViolationException, Exception {107 Mockito.when(result.hasErrors()).thenReturn(false);108 Mockito.doThrow(new Exception()).when(roleService).addRole(Mockito.any(Role.class));109 110 final String view = roleController.addingRole(role, result, model);111 112 assertEquals(view, "admin/admin");113 114 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();115 Mockito.verify(roleService, VerificationModeFactory.times(1)).addRole(Mockito.any(Role.class));116 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", null);117 Mockito.verifyNoMoreInteractions(result);118 Mockito.verifyNoMoreInteractions(roleService);119 Mockito.verifyNoMoreInteractions(model);120 }121 122 @Test 123 public void addingRoleReturnValueWhenResultHasNoErrorsAndIsSuccessful() throws ConstraintViolationException, Exception {124 Mockito.when(result.hasErrors()).thenReturn(false);125 Mockito.when(roleService.addRole(Mockito.any(Role.class))).thenReturn(true);126 127 final String view = roleController.addingRole(role, result, model);128 129 assertEquals(view, "admin/admin");130 131 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();132 Mockito.verify(roleService, VerificationModeFactory.times(1)).addRole(Mockito.any(Role.class));133 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("message", "role.added.success");134 Mockito.verifyNoMoreInteractions(result);135 Mockito.verifyNoMoreInteractions(roleService);136 Mockito.verifyNoMoreInteractions(model);137 }138 139 @Test 140 public void addingRoleReturnValueWhenResultHasNoErrorsAndIsNotSuccessful() throws ConstraintViolationException, Exception {141 Mockito.when(result.hasErrors()).thenReturn(false);142 Mockito.when(roleService.addRole(Mockito.any(Role.class))).thenReturn(false);143 144 final String view = roleController.addingRole(role, result, model);145 146 assertEquals(view, "admin/admin");147 148 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();149 Mockito.verify(roleService, VerificationModeFactory.times(1)).addRole(Mockito.any(Role.class));150 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "role.already.exists");151 Mockito.verifyNoMoreInteractions(result);152 Mockito.verifyNoMoreInteractions(roleService);153 Mockito.verifyNoMoreInteractions(model);154 }155 156 @Test157 public void editRolePageReturnValueWhenThrowsHibernateException() {158 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(roleService).findById(1L);159 160 final String view = roleController.editRolePage(1L, model);161 162 assertEquals(view, "role/edit-role");163 164 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());165 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "An error occured accessing the database ...");166 Mockito.verifyNoMoreInteractions(roleService);167 Mockito.verifyNoMoreInteractions(model);168 }169 170 @Test171 public void editRolePageReturnValueWhenThrowsRuntimeException() {172 Mockito.doThrow(new RuntimeException("Exception")).when(roleService).findById(1L);173 174 final String view = roleController.editRolePage(1L, model);175 176 assertEquals(view, "role/edit-role");177 178 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());179 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");180 Mockito.verifyNoMoreInteractions(roleService);181 Mockito.verifyNoMoreInteractions(model);182 }183 184 @Test185 public void editRolePageReturnValueWhenSuccessful() {186 Mockito.when(roleService.findById(1L)).thenReturn(role);187 188 final String view = roleController.editRolePage(1L, model);189 190 assertEquals(view, "role/edit-role");191 192 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());193 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(Role.class));194 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(User.class));195 Mockito.verifyNoMoreInteractions(roleService);196 Mockito.verifyNoMoreInteractions(model);197 }198 199 @Test 200 public void editingRoleReturnValueWhenResultHasError() {201 Mockito.when(result.hasErrors()).thenReturn(true);202 203 final String view = roleController.editingRole(role, result, user, userResult, 1L, model);204 205 assertEquals(view, "role/edit-role");206 207 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();208 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(Role.class));209 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "error.have.occured");210 Mockito.verifyNoMoreInteractions(result);211 Mockito.verifyNoMoreInteractions(model);212 }213 214 @Test 215 public void editingRoleReturnValueWhenResultHasNoErrorsAndThrowsConstraintViolation() throws ConstraintViolationException, Exception {216 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);217 Mockito.when(result.hasErrors()).thenReturn(false);218 Mockito.doThrow(cve).when(roleService).updateRole(Mockito.any(Role.class));219220 final String view = roleController.editingRole(role, result, user, userResult, 1L, model);221 assertEquals(view, "admin/admin");222 223 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();224 Mockito.verify(roleService, VerificationModeFactory.times(1)).updateRole(Mockito.any(Role.class));225 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", cve.getMessage());226 Mockito.verifyNoMoreInteractions(result);227 Mockito.verifyNoMoreInteractions(roleService);228 Mockito.verifyNoMoreInteractions(model);229 }230 231 @Test 232 public void editingRoleReturnValueWhenResultHasNoErrorsAndThrowsException() throws ConstraintViolationException, Exception {233 Mockito.when(result.hasErrors()).thenReturn(false);234 Mockito.doThrow(new Exception()).when(roleService).updateRole(Mockito.any(Role.class));235 236 final String view = roleController.editingRole(role, result, user, userResult, 1L, model);237 238 assertEquals(view, "admin/admin");239 240 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();241 Mockito.verify(roleService, VerificationModeFactory.times(1)).updateRole(Mockito.any(Role.class));242 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", null);243 Mockito.verifyNoMoreInteractions(result);244 Mockito.verifyNoMoreInteractions(roleService);245 Mockito.verifyNoMoreInteractions(model);246 }247 248 @Test 249 public void editingRoleReturnValueWhenResultHasNoErrorsAndIsSuccessful() throws ConstraintViolationException, Exception {250 Mockito.when(result.hasErrors()).thenReturn(false);251 Mockito.when(roleService.updateRole(Mockito.any(Role.class))).thenReturn(true);252 253 final String view = roleController.editingRole(role, result, user, userResult, 1L, model);254 255 assertEquals(view, "admin/admin");256 257 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();258 Mockito.verify(roleService, VerificationModeFactory.times(1)).updateRole(Mockito.any(Role.class));259 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("message", "role.update.success");260 Mockito.verifyNoMoreInteractions(result);261 Mockito.verifyNoMoreInteractions(roleService);262 Mockito.verifyNoMoreInteractions(model);263 }264 265 @Test 266 public void editingRoleReturnValueWhenResultHasNoErrorsAndIsNotSuccessful() throws ConstraintViolationException, Exception {267 Mockito.when(result.hasErrors()).thenReturn(false);268 Mockito.when(roleService.updateRole(Mockito.any(Role.class))).thenReturn(false);269 270 final String view = roleController.editingRole(role, result, user, userResult, 1L, model);271 272 assertEquals(view, "admin/admin");273 274 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();275 Mockito.verify(roleService, VerificationModeFactory.times(1)).updateRole(Mockito.any(Role.class));276 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "role.does.not.exists");277 Mockito.verifyNoMoreInteractions(result);278 Mockito.verifyNoMoreInteractions(roleService);279 Mockito.verifyNoMoreInteractions(model);280 }281 282 @Test283 public void deleteRolePageReturnValueWhenThrowsHibernateException() {284 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(roleService).findById(1L);285 286 final String view = roleController.deleteRolePage(1L, model);287 288 assertEquals(view, "redirect:/role/list");289 290 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());291 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "An error occured accessing the database ...");292 Mockito.verifyNoMoreInteractions(roleService);293 Mockito.verifyNoMoreInteractions(model);294 }295 296 @Test297 public void deleteRolePageReturnValueWhenThrowsRuntimeException() {298 Mockito.doThrow(new RuntimeException("Exception")).when(roleService).findById(1L);299 300 final String view = roleController.deleteRolePage(1L, model);301 302 assertEquals(view, "redirect:/role/list");303 304 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());305 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");306 Mockito.verifyNoMoreInteractions(roleService);307 Mockito.verifyNoMoreInteractions(model);308 }309 310 @Test311 public void deleteRolePageReturnValueWhenThrowsException() throws Exception {312 Mockito.when(roleService.findById(1L)).thenReturn(role);313 Mockito.doThrow(new Exception("Exception")).when(roleService).deleteRole(Mockito.any(Role.class));314 315 final String view = roleController.deleteRolePage(1L, model);316 317 assertEquals(view, "redirect:/role/list");318 319 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());320 Mockito.verify(roleService, VerificationModeFactory.times(1)).deleteRole(Mockito.any(Role.class));321 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");322 Mockito.verifyNoMoreInteractions(roleService);323 Mockito.verifyNoMoreInteractions(model);324 }325 326 @Test327 public void deleteRolePageReturnValueWhenSuccessful() throws Exception {328 Mockito.when(roleService.findById(1L)).thenReturn(role);329 Mockito.when(roleService.deleteRole(Mockito.any(Role.class))).thenReturn(true);330 331 final String view = roleController.deleteRolePage(1L, model);332 333 assertEquals(view, "redirect:/role/list");334 335 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());336 Mockito.verify(roleService, VerificationModeFactory.times(1)).deleteRole(Mockito.any(Role.class));337 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("message", "role.delete.success");338 Mockito.verifyNoMoreInteractions(roleService);339 Mockito.verifyNoMoreInteractions(model);340 }341 342 @Test343 public void deleteRolePageReturnValueWhenNotSuccessful() throws Exception {344 Mockito.when(roleService.findById(1L)).thenReturn(role);345 Mockito.when(roleService.deleteRole(Mockito.any(Role.class))).thenReturn(false);346 347 final String view = roleController.deleteRolePage(1L, model);348 349 assertEquals(view, "redirect:/role/list");350 351 Mockito.verify(roleService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());352 Mockito.verify(roleService, VerificationModeFactory.times(1)).deleteRole(Mockito.any(Role.class));353 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "role.delete.error");354 Mockito.verifyNoMoreInteractions(roleService);355 Mockito.verifyNoMoreInteractions(model);356 }357 358 @Test359 public void listOfRolesPageReturnValueWhenThrowsHibernateException() {360 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(roleService).findAllRoles();361 362 final String view = roleController.listOfRoles(model);363 364 assertEquals(view, "role/list-roles");365 366 Mockito.verify(roleService, VerificationModeFactory.times(1)).findAllRoles();367 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "An error occured accessing the database ...");368 Mockito.verifyNoMoreInteractions(roleService);369 Mockito.verifyNoMoreInteractions(model);370 }371 372 @Test373 public void listOfRolesPageReturnValueWhenThrowsRuntimeException() {374 Mockito.doThrow(new RuntimeException("Exception")).when(roleService).findAllRoles();375 376 final String view = roleController.listOfRoles(model);377 378 assertEquals(view, "role/list-roles");379 380 Mockito.verify(roleService, VerificationModeFactory.times(1)).findAllRoles();381 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");382 Mockito.verifyNoMoreInteractions(roleService);383 Mockito.verifyNoMoreInteractions(model);384 }385 386 @Test387 public void listOfRolesPageReturnValueWhenSuccessful() {388 List<Role> lst = new ArrayList<Role>();389 lst.add(role);390 391 Mockito.when(roleService.findAllRoles()).thenReturn(lst);392 393 final String view = roleController.listOfRoles(model);394 395 assertEquals(view, "role/list-roles");396 397 Mockito.verify(roleService, VerificationModeFactory.times(1)).findAllRoles();398 Mockito.verifyNoMoreInteractions(roleService);399 }400}
...
Source:UserControllerTest.java
...64 65 assertEquals(view, "user/add-user");66 67 Mockito.verify(model).addAttribute(Mockito.anyString(), Mockito.any(UserDto.class));68 Mockito.verifyNoMoreInteractions(model);69 }70 71 @Test 72 public void addingUserReturnValueWhenResultHasError() {73 Mockito.when(result.hasErrors()).thenReturn(true);74 75 final String view = userController.addingUser(userDto, result, model);76 77 assertEquals(view, "user/add-user");78 79 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();80 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(UserDto.class));81 Mockito.verify(model).addAttribute("errorMessage", "error.have.occured");82 Mockito.verifyNoMoreInteractions(result);83 Mockito.verifyNoMoreInteractions(model);84 }85 86 @Test 87 public void addingUserReturnValueWhenResultHasNoErrorsAndThrowsConstraintViolation() throws ConstraintViolationException, Exception {88 userDto.setFirstName("Null is not working so I will make myself incredibly long so i trigger the length constraint");89 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new user ... ", null);90 Mockito.when(result.hasErrors()).thenReturn(false);91 Mockito.doThrow(cve).when(userService).addUser(Mockito.any(User.class));9293 final String view = userController.addingUser(userDto, result, model);94 assertEquals(view, "index");95 96 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();97 Mockito.verify(userService, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));98 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", cve.getMessage());99 Mockito.verifyNoMoreInteractions(result);100 Mockito.verifyNoMoreInteractions(userService);101 Mockito.verifyNoMoreInteractions(model);102 }103 104 @Test 105 public void addingUserReturnValueWhenResultHasNoErrorsAndThrowsException() throws ConstraintViolationException, Exception {106 Mockito.when(result.hasErrors()).thenReturn(false);107 Mockito.doThrow(new Exception()).when(userService).addUser(Mockito.any(User.class));108 109 final String view = userController.addingUser(userDto, result, model);110 111 assertEquals(view, "index");112 113 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();114 Mockito.verify(userService, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));115 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", null);116 Mockito.verifyNoMoreInteractions(result);117 Mockito.verifyNoMoreInteractions(userService);118 Mockito.verifyNoMoreInteractions(model);119 }120 121 @Test 122 public void addingUserReturnValueWhenResultHasNoErrorsAndIsSuccessful() throws ConstraintViolationException, Exception {123 Mockito.when(result.hasErrors()).thenReturn(false);124 Mockito.when(userService.addUser(Mockito.any(User.class))).thenReturn(true);125 126 final String view = userController.addingUser(userDto, result, model);127 128 assertEquals(view, "index");129 130 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();131 Mockito.verify(userService, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));132 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("message", "user.added.success");133 Mockito.verifyNoMoreInteractions(result);134 Mockito.verifyNoMoreInteractions(userService);135 Mockito.verifyNoMoreInteractions(model);136 }137 138 @Test 139 public void addingUserReturnValueWhenResultHasNoErrorsAndIsNotSuccessful() throws ConstraintViolationException, Exception {140 Mockito.when(result.hasErrors()).thenReturn(false);141 Mockito.when(userService.addUser(Mockito.any(User.class))).thenReturn(false);142 143 final String view = userController.addingUser(userDto, result, model);144 145 assertEquals(view, "index");146 147 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();148 Mockito.verify(userService, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));149 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "user.already.exists");150 Mockito.verifyNoMoreInteractions(result);151 Mockito.verifyNoMoreInteractions(userService);152 Mockito.verifyNoMoreInteractions(model);153 }154 155 @Test156 public void editUserPageReturnValueWhenThrowsHibernateException() {157 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(userService).findById(1L);158 159 final String view = userController.editUserPage(1L, model);160 161 assertEquals(view, "user/edit-user");162 163 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());164 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "An error occured accessing the database ...");165 Mockito.verifyNoMoreInteractions(userService);166 Mockito.verifyNoMoreInteractions(model);167 }168 169 @Test170 public void editUserPageReturnValueWhenThrowsRuntimeException() {171 Mockito.doThrow(new RuntimeException("Exception")).when(userService).findById(1L);172 173 final String view = userController.editUserPage(1L, model);174 175 assertEquals(view, "user/edit-user");176 177 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());178 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");179 Mockito.verifyNoMoreInteractions(userService);180 Mockito.verifyNoMoreInteractions(model);181 }182 183 @Test184 public void editUserPageReturnValueWhenSuccessful() {185 Mockito.when(userService.findById(1L)).thenReturn(user);186 187 final String view = userController.editUserPage(1L, model);188 189 assertEquals(view, "user/edit-user");190 191 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());192 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute(Mockito.anyString(), Mockito.any(UserDto.class));193 Mockito.verifyNoMoreInteractions(userService);194 Mockito.verifyNoMoreInteractions(model);195 }196 197 @Test 198 public void editingUserReturnValueWhenResultHasError() {199 Mockito.when(result.hasErrors()).thenReturn(true);200 201 final String view = userController.editingUser(userDto, result, 1L, model);202 203 assertEquals(view, "user/edit-user");204 205 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();206 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(UserDto.class));207 Mockito.verify(model).addAttribute("errorMessage", "error.have.occured");208 Mockito.verifyNoMoreInteractions(result);209 Mockito.verifyNoMoreInteractions(model);210 }211 212 @Test 213 public void editingUserReturnValueWhenResultHasNoErrorsAndThrowsConstraintViolation() throws ConstraintViolationException, Exception {214 userDto.setFirstName("Null is not working so I will make myself incredibly long so i trigger the length constraint");215 ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update user ... ", null);216 Mockito.when(result.hasErrors()).thenReturn(false);217 Mockito.doThrow(cve).when(userService).updateUser(Mockito.any(User.class));218219 final String view = userController.editingUser(userDto, result, 1L, model);220 assertEquals(view, "admin/admin");221 222 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();223 Mockito.verify(userService, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));224 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", cve.getMessage());225 Mockito.verifyNoMoreInteractions(result);226 Mockito.verifyNoMoreInteractions(userService);227 Mockito.verifyNoMoreInteractions(model);228 }229 230 @Test 231 public void editingUserReturnValueWhenResultHasNoErrorsAndThrowsException() throws ConstraintViolationException, Exception {232 Mockito.when(result.hasErrors()).thenReturn(false);233 Mockito.doThrow(new Exception()).when(userService).updateUser(Mockito.any(User.class));234 235 final String view = userController.editingUser(userDto, result, 1L, model);236 237 assertEquals(view, "admin/admin");238 239 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();240 Mockito.verify(userService, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));241 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", null);242 Mockito.verifyNoMoreInteractions(result);243 Mockito.verifyNoMoreInteractions(userService);244 Mockito.verifyNoMoreInteractions(model);245 }246 247 @Test 248 public void editingUserReturnValueWhenResultHasNoErrorsAndIsSuccessful() throws ConstraintViolationException, Exception {249 Mockito.when(result.hasErrors()).thenReturn(false);250 Mockito.when(userService.updateUser(Mockito.any(User.class))).thenReturn(true);251 252 final String view = userController.editingUser(userDto, result, 1L, model);253 254 assertEquals(view, "admin/admin");255 256 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();257 Mockito.verify(userService, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));258 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("message", "user.update.success");259 Mockito.verifyNoMoreInteractions(result);260 Mockito.verifyNoMoreInteractions(userService);261 Mockito.verifyNoMoreInteractions(model);262 }263 264 @Test 265 public void editingUserReturnValueWhenResultHasNoErrorsAndIsNotSuccessful() throws ConstraintViolationException, Exception {266 Mockito.when(result.hasErrors()).thenReturn(false);267 Mockito.when(userService.updateUser(Mockito.any(User.class))).thenReturn(false);268 269 final String view = userController.editingUser(userDto, result, 1L, model);270 271 assertEquals(view, "admin/admin");272 273 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();274 Mockito.verify(userService, VerificationModeFactory.times(1)).updateUser(Mockito.any(User.class));275 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "user.does.not.exists");276 Mockito.verifyNoMoreInteractions(result);277 Mockito.verifyNoMoreInteractions(userService);278 Mockito.verifyNoMoreInteractions(model);279 }280 281 @Test282 public void deleteUserPageReturnValueWhenThrowsHibernateException() {283 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(userService).findById(1L);284 285 final String view = userController.deleteUserPage(1L, model);286 287 assertEquals(view, "redirect:/user/list");288 289 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());290 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "An error occured accessing the database ...");291 Mockito.verifyNoMoreInteractions(userService);292 Mockito.verifyNoMoreInteractions(model);293 }294 295 @Test296 public void deleteUserPageReturnValueWhenThrowsRuntimeException() {297 Mockito.doThrow(new RuntimeException("Exception")).when(userService).findById(1L);298 299 final String view = userController.deleteUserPage(1L, model);300 301 assertEquals(view, "redirect:/user/list");302 303 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());304 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");305 Mockito.verifyNoMoreInteractions(userService);306 Mockito.verifyNoMoreInteractions(model);307 }308 309 @Test310 public void deleteUserPageReturnValueWhenThrowsException() throws Exception {311 Mockito.when(userService.findById(1L)).thenReturn(user);312 Mockito.doThrow(new Exception("Exception")).when(userService).deleteUser(Mockito.any(User.class));313 314 final String view = userController.deleteUserPage(1L, model);315 316 assertEquals(view, "redirect:/user/list");317 318 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());319 Mockito.verify(userService, VerificationModeFactory.times(1)).deleteUser(Mockito.any(User.class));320 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");321 Mockito.verifyNoMoreInteractions(userService);322 Mockito.verifyNoMoreInteractions(model);323 }324 325 @Test326 public void deleteUserPageReturnValueWhenSuccessful() throws Exception {327 Mockito.when(userService.findById(1L)).thenReturn(user);328 Mockito.when(userService.deleteUser(Mockito.any(User.class))).thenReturn(true);329 330 final String view = userController.deleteUserPage(1L, model);331 332 assertEquals(view, "redirect:/user/list");333 334 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());335 Mockito.verify(userService, VerificationModeFactory.times(1)).deleteUser(Mockito.any(User.class));336 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("message", "user.delete.success");337 Mockito.verifyNoMoreInteractions(userService);338 Mockito.verifyNoMoreInteractions(model);339 }340 341 @Test342 public void deleteUserPageReturnValueWhenNotSuccessful() throws Exception {343 Mockito.when(userService.findById(1L)).thenReturn(user);344 Mockito.when(userService.deleteUser(Mockito.any(User.class))).thenReturn(false);345 346 final String view = userController.deleteUserPage(1L, model);347 348 assertEquals(view, "redirect:/user/list");349 350 Mockito.verify(userService, VerificationModeFactory.times(1)).findById(Mockito.anyLong());351 Mockito.verify(userService, VerificationModeFactory.times(1)).deleteUser(Mockito.any(User.class));352 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "user.delete.error");353 Mockito.verifyNoMoreInteractions(userService);354 Mockito.verifyNoMoreInteractions(model);355 }356 357 @Test358 public void listOfUserPageReturnValueWhenThrowsHibernateException() {359 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(userService).findAllUsers();360 361 final String view = userController.listOfUsers(model);362 363 assertEquals(view, "user/list-users");364 365 Mockito.verify(userService, VerificationModeFactory.times(1)).findAllUsers();366 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "An error occured accessing the database ...");367 Mockito.verifyNoMoreInteractions(userService);368 Mockito.verifyNoMoreInteractions(model);369 }370 371 @Test372 public void listOfUserPageReturnValueWhenThrowsRuntimeException() {373 Mockito.doThrow(new RuntimeException("Exception")).when(userService).findAllUsers();374 375 final String view = userController.listOfUsers(model);376 377 assertEquals(view, "user/list-users");378 379 Mockito.verify(userService, VerificationModeFactory.times(1)).findAllUsers();380 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "Exception");381 Mockito.verifyNoMoreInteractions(userService);382 Mockito.verifyNoMoreInteractions(model);383 }384 385 @Test386 public void listOfUserPageReturnValueWhenSuccessful() {387 List<User> lst = new ArrayList<User>();388 lst.add(user);389 390 Mockito.when(userService.findAllUsers()).thenReturn(lst);391 392 final String view = userController.listOfUsers(model);393 394 assertEquals(view, "user/list-users");395 396 Mockito.verify(userService, VerificationModeFactory.times(1)).findAllUsers();397 Mockito.verifyNoMoreInteractions(userService);398 }399}
...
Source:RoleServiceTest.java
...109// role.setRole(null);110// roleService.addRole(role);111// 112// Mockito.verify(roleRepository, VerificationModeFactory.times(1)).addRole(Mockito.any(Role.class));113// Mockito.verifyNoMoreInteractions(roleRepository);114// }115// 116// @Test(expected = ConstraintViolationException.class)117// public void addNewRoleWhenUserIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {118// ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not add new role ... ", null, null); 119// Mockito.doThrow(cve).when(roleRepository).addRole(Mockito.any(Role.class));120// 121// role.setUser(null);122// roleService.addRole(role);123// 124// Mockito.verify(roleRepository, VerificationModeFactory.times(1)).addRole(Mockito.any(Role.class));125// Mockito.verifyNoMoreInteractions(roleRepository);126// }127 128 /**129 * Add New Role Tests: Add New Role is successful.130 * @throws ConstraintViolationException 131 * @throws Exception 132 */133 134 @Test135 public void addNewRoleWhenRoleIsValidAndDoesNotAlreadyExist() throws ConstraintViolationException, Exception { 136 Mockito.when(roleRepository.findById(Mockito.anyLong())).thenReturn(null);137 Mockito.doNothing().when(roleRepository).addRole(Mockito.any(Role.class));138 try {139 Boolean b = roleService.addRole(role);140 141 assertTrue(b);142 } catch (Exception e) {143 fail("User should add successfully");144 }145 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());146 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).addRole(Mockito.any(Role.class));147 Mockito.verifyNoMoreInteractions(roleRepository);148 }149 150 @Test151 public void addNewRoleWhenRoleIsValidAndAlreadyExist() throws ConstraintViolationException, Exception { 152 Mockito.when(roleRepository.findById(Mockito.anyLong())).thenReturn(role);153 try {154 Boolean b = roleService.addRole(role);155 156 assertFalse(b);157 } catch (Exception e) {158 fail("User should add successfully");159 }160 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());161 Mockito.verifyNoMoreInteractions(roleRepository);162 }163 164 /**165 * Update Role Tests: Should cover throwing a new Exception.166 * 167 * @throws ConstraintViolationException168 * @throws Exception169 */170 @Test(expected = Exception.class)171 public void updateRoleWhenRoleIsNullShouldThrowException() throws ConstraintViolationException, Exception {172 roleService.updateRole(null);173 }174 175// @Test(expected = ConstraintViolationException.class)176// public void updateRoleWhenRoleIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {177// ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update role ... ", null, null); 178// Mockito.doThrow(cve).when(roleRepository).updateRole(Mockito.any(Role.class));179//180// role.setRole(null);181// roleService.updateRole(role);182// 183// Mockito.verify(roleRepository, VerificationModeFactory.times(1)).updateRole(Mockito.any(Role.class));184// Mockito.verifyNoMoreInteractions(roleRepository);185// }186// 187// @Test(expected = ConstraintViolationException.class)188// public void updateRoleWhenUserIsNullConstraintShouldThrowConstraintViolation() throws ConstraintViolationException, Exception {189// ConstraintViolationException cve = new ConstraintViolationException("Constraint Violation: Could not update role ... ", null, null); 190// Mockito.doThrow(cve).when(roleRepository).updateRole(Mockito.any(Role.class));191// 192// role.setUser(null);193// roleService.updateRole(role);194// 195// Mockito.verify(roleRepository, VerificationModeFactory.times(1)).updateRole(Mockito.any(Role.class));196// Mockito.verifyNoMoreInteractions(roleRepository);197// }198 199 /**200 * Update Role Tests: Update Role is successful.201 * @throws ConstraintViolationException 202 * @throws Exception 203 */204 205 @Test206 public void updateRoleWhenRoleIsValidAndAlreadyExists() throws ConstraintViolationException, Exception { 207 Mockito.when(roleRepository.findById(Mockito.anyLong())).thenReturn(role);208 Mockito.doNothing().when(roleRepository).updateRole(Mockito.any(Role.class));209 try {210 Boolean b = roleService.updateRole(role);211 212 assertTrue(b);213 } catch (Exception e) {214 fail("User should update successfully");215 }216 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());217 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).updateRole(Mockito.any(Role.class));218 Mockito.verifyNoMoreInteractions(roleRepository);219 }220 221 @Test222 public void updateRoleWhenRoleIsValidAndDoesNotAlreadyExist() throws ConstraintViolationException, Exception { 223 Mockito.when(roleRepository.findById(Mockito.anyLong())).thenReturn(null);224 try {225 Boolean b = roleService.updateRole(role);226 227 assertFalse(b);228 } catch (Exception e) {229 fail("User should update successfully");230 }231 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());232 Mockito.verifyNoMoreInteractions(roleRepository);233 }234 235 /**236 * Delete Role Tests: Should cover throwing a new Exception.237 * 238 * @throws Exception239 */240 @Test(expected = Exception.class)241 public void deleteRoleWhenRoleIsNullShouldThrowException() throws Exception {242 roleService.deleteRole(null);243 }244 245 @Test246 public void deleteRoleWhenRoleIsValidAndRoleIsDeleted() throws Exception {247 Mockito.when(roleRepository.findById(Mockito.anyLong())).thenReturn(role).thenReturn(null);248 Mockito.doNothing().when(roleRepository).deleteRole(Mockito.any(Role.class));249 try {250 Boolean b = roleService.deleteRole(role);251 252 assertTrue(b);253 } catch (Exception e) {254 fail("Role should add successfully");255 }256 Mockito.verify(roleRepository, VerificationModeFactory.times(2)).findById(Mockito.anyLong());257 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).deleteRole(Mockito.any(Role.class));258 Mockito.verifyNoMoreInteractions(roleRepository);259 }260 261 @Test262 public void deleteRoleWhenRoleIsValidAndRoleDoesNotExist() throws Exception {263 Mockito.when(roleRepository.findById(Mockito.anyLong())).thenReturn(null);264 try {265 Boolean b = roleService.deleteRole(role);266 267 assertFalse(b);268 } catch (Exception e) {269 fail("Role should add successfully");270 }271 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());272 Mockito.verifyNoMoreInteractions(roleRepository);273 }274 275 @Test276 public void deleteRoleWhenRoleIsValidAndDoesNotDelete() throws Exception {277 Mockito.when(roleRepository.findById(Mockito.anyLong())).thenReturn(role).thenReturn(role);278 Mockito.doNothing().when(roleRepository).deleteRole(Mockito.any(Role.class));279 try {280 Boolean b = roleService.deleteRole(role);281 282 assertFalse(b);283 } catch (Exception e) {284 fail("Role should add successfully");285 }286 Mockito.verify(roleRepository, VerificationModeFactory.times(2)).findById(Mockito.anyLong());287 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).deleteRole(Mockito.any(Role.class));288 Mockito.verifyNoMoreInteractions(roleRepository);289 }290 291 @Test(expected = RuntimeException.class)292 public void findRoleByIdThrowsRuntimeException() throws RuntimeException { 293 Mockito.doThrow(new RuntimeException()).when(roleRepository).findById(Mockito.anyLong());294 295 roleService.findById(1L);296 297 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());298 Mockito.verifyNoMoreInteractions(roleRepository);299 }300 301 @Test(expected = HibernateException.class)302 public void findRoleByIdThrowsHibernateException() throws HibernateException { 303 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(roleRepository).findById(Mockito.anyLong());304 305 roleService.findById(1L);306 307 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());308 Mockito.verifyNoMoreInteractions(roleRepository);309 }310 311 @Test312 public void findRoleByIdIsSuccessful() {313 role.setId(1L); 314 Mockito.doReturn(role).when(roleRepository).findById(Mockito.anyLong()); 315 try {316 Role r = roleService.findById(1L);317 318 assertNotNull(r);319 } catch (Exception e) {320 fail("No Exceptions should be thrown: user should not be null.");321 }322 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());323 Mockito.verifyNoMoreInteractions(roleRepository);324 }325 326 @Test327 public void findRoleByIdFails(){ 328 Mockito.doReturn(null).when(roleRepository).findById(Mockito.anyLong()); 329 try {330 Role r = roleService.findById(1L);331 332 assertNull(r);333 } catch (Exception e) {334 fail("No Exceptions should be thrown: user should be null.");335 }336 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());337 Mockito.verifyNoMoreInteractions(roleRepository);338 }339 340 @Test(expected = RuntimeException.class)341 public void findByRoleThrowsRuntimeException() throws RuntimeException { 342 Mockito.doThrow(new RuntimeException()).when(roleRepository).findByRole(Mockito.anyLong());343 344 roleService.findByRole(1L);345 346 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findByRole(Mockito.anyLong());347 Mockito.verifyNoMoreInteractions(roleRepository);348 }349 350 @Test(expected = HibernateException.class)351 public void findByRoleThrowsHibernateException() throws HibernateException { 352 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(roleRepository).findByRole(Mockito.anyLong());353 354 roleService.findByRole(1L);355 356 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findByRole(Mockito.anyLong());357 Mockito.verifyNoMoreInteractions(roleRepository);358 }359 360 @Test361 public void findByRoleIsSuccessful() {362 List<Role> lst = new ArrayList<Role>();363 lst.add(role);364 365 Mockito.when(roleRepository.findByRole(Mockito.anyLong())).thenReturn(lst); 366 try {367 List<Role> r = roleService.findByRole(1L);368 369 assertNotNull(r);370 } catch (Exception e) {371 fail("No Exceptions should be thrown: user should not be null.");372 }373 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findByRole(Mockito.anyLong());374 Mockito.verifyNoMoreInteractions(roleRepository);375 }376 377 @Test378 public void findByRoleFails(){379 Mockito.when(roleRepository.findByRole(Mockito.anyLong())).thenReturn(null);380 try {381 List<Role> r = roleService.findByRole(1L);382 383 assertNull(r);384 } catch (Exception e) {385 fail("No Exceptions should be thrown: user should be null.");386 }387 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findByRole(Mockito.anyLong());388 Mockito.verifyNoMoreInteractions(roleRepository);389 }390 391 @Test(expected = RuntimeException.class)392 public void findAllRolesThrowsRuntimeException() throws RuntimeException {393 Mockito.doThrow(new RuntimeException()).when(roleRepository).findAllRoles();394 395 roleService.findAllRoles();396 397 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findAllRoles();398 Mockito.verifyNoMoreInteractions(roleRepository);399 }400 401 @Test(expected = HibernateException.class)402 public void findAllRolesThrowsHibernateException() throws HibernateException {403 Mockito.doThrow(new HibernateException("An error occured accessing the database ...")).when(roleRepository).findAllRoles();404 405 roleService.findAllRoles();406 407 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findAllRoles();408 Mockito.verifyNoMoreInteractions(roleRepository);409 }410 411 @Test412 public void findAllRolesIsSuccessful() {413 List<Role> lst = new ArrayList<Role>();414 lst.add(role);415 416 Mockito.when(roleRepository.findAllRoles()).thenReturn(lst);417 try {418 List<Role> r = roleService.findAllRoles();419 420 assertNotNull(r);421 } catch (Exception e) {422 fail("No Exceptions should be thrown: user should not be null.");423 }424 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findAllRoles();425 Mockito.verifyNoMoreInteractions(roleRepository);426 }427 428 @Test429 public void findAllRolesFails(){430 Mockito.when(roleRepository.findAllRoles()).thenReturn(null); 431 try {432 List<Role> r = roleService.findAllRoles();433 434 assertNull(r);435 } catch (Exception e) {436 fail("No Exceptions should be thrown: user should be null.");437 }438 Mockito.verify(roleRepository, VerificationModeFactory.times(1)).findAllRoles();439 Mockito.verifyNoMoreInteractions(roleRepository);440 }441}
...
Source:AccessControllerTest.java
...67 assertEquals(view, "access/login");68 69 Mockito.verify(request, VerificationModeFactory.times(1)).getSession();70 Mockito.verify(session, VerificationModeFactory.times(1)).getAttribute("SPRING_SECURITY_LAST_EXCEPTION");71 Mockito.verifyNoMoreInteractions(request);72 Mockito.verifyNoMoreInteractions(session);73 }74 75 @Test76 public void loginReturnValueWhenErrorIsNotEmptyAndExceptionIsUnknown() {77 Mockito.when(request.getSession()).thenReturn(session);78 Mockito.when(session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION")).thenReturn(new Exception("Test Exception"));79 80 final String view = accessController.login(model, request);81 82 assertEquals(view, "access/login");83 84 Mockito.verify(model).addAttribute("errorMessage", "Test Exception");85 Mockito.verify(request, VerificationModeFactory.times(1)).getSession();86 Mockito.verify(session, VerificationModeFactory.times(1)).getAttribute("SPRING_SECURITY_LAST_EXCEPTION");87 Mockito.verifyNoMoreInteractions(model);88 Mockito.verifyNoMoreInteractions(request);89 Mockito.verifyNoMoreInteractions(session);90 }91 92 @Test93 public void loginReturnValueWhenErrorIsNotEmptyAndExceptionIsNullPointer() {94 Mockito.when(request.getSession()).thenReturn(session);95 Mockito.when(session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION")).thenReturn(new Exception("java.lang.NullPointerException"));96 97 final String view = accessController.login(model, request);98 99 assertEquals(view, "access/login");100 101 Mockito.verify(model).addAttribute("errorMessage", "login.failure.username.notfound");102 Mockito.verify(request, VerificationModeFactory.times(1)).getSession();103 Mockito.verify(session, VerificationModeFactory.times(1)).getAttribute("SPRING_SECURITY_LAST_EXCEPTION");104 Mockito.verifyNoMoreInteractions(model);105 Mockito.verifyNoMoreInteractions(request);106 Mockito.verifyNoMoreInteractions(session);107 }108 109 @Test110 public void loginReturnValueWhenErrorIsNotEmptyAndExceptionIsBadCredentials() {111 Mockito.when(request.getSession()).thenReturn(session);112 Mockito.when(session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION")).thenReturn(new Exception("Bad Credentials"));113 114 final String view = accessController.login(model, request);115 116 assertEquals(view, "access/login");117 118 Mockito.verify(model).addAttribute("errorMessage", "login.failure.username.password.incorrect");119 Mockito.verify(request, VerificationModeFactory.times(1)).getSession();120 Mockito.verify(session, VerificationModeFactory.times(1)).getAttribute("SPRING_SECURITY_LAST_EXCEPTION");121 Mockito.verifyNoMoreInteractions(model);122 Mockito.verifyNoMoreInteractions(request);123 Mockito.verifyNoMoreInteractions(session);124 }125 126 @Test127 public void loginErrorReturnValue() {128 final String view = accessController.loginError(model);129 130 assertEquals(view, "access/login");131 }132 133 @Test134 public void logoutReturnValue() {135 final String view = accessController.logout(model);136 137 assertEquals(view, "access/login");138 }139 140 @Test141 public void logoutSuccessReturnValue() {142 final String view = accessController.logoutSuccess(model);143 144 assertEquals(view, "access/login");145 }146 147 @Test148 public void logoutErrorReturnValue() {149 final String view = accessController.logoutError(model);150 151 assertEquals(view, null);152 }153 154 @Test155 public void deniedReturnValue() {156 final String view = accessController.denied(model);157 158 assertEquals(view, "access/denied");159 160 Mockito.verify(model).addAttribute("errorMessage", "access.denied.message");161 Mockito.verifyNoMoreInteractions(model);162 }163 164 @Test165 public void signupReturnValue() {166 final String view = accessController.signup(model);167 168 assertEquals(view, "access/signup");169 170 Mockito.verify(model).addAttribute(Mockito.anyString(), Mockito.any(UserDto.class));171 Mockito.verifyNoMoreInteractions(model);172 }173 174 @Test175 public void createAccountReturnValueWhenBindingResultHasErrors() {176 Mockito.when(result.hasErrors()).thenReturn(true);177 178 final String view = accessController.createAccount(userDto, result, request, model);179 180 assertEquals(view, "access/signup");181 182 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();183 // Not sure why this is needed two times, it should only be needed once.184 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(UserDto.class));185 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "error.have.occured");186 Mockito.verifyNoMoreInteractions(result);187 Mockito.verifyNoMoreInteractions(model);188 }189 190 @Test191 public void createAccountReturnValueWhenBindingResultHasNoErrorsUserDoesNotExistsAndUsernamesDoNotMatch() {192 Mockito.when(result.hasErrors()).thenReturn(false);193 194 userDto.setUserName("test@fake.com");195 userDto.setReUserName("user@fake.com");196 197 final String view = accessController.createAccount(userDto, result, request, model);198 199 assertEquals(view, "access/signup");200 201 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();202 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "signup.invalid.username.notmatching");203 Mockito.verifyNoMoreInteractions(result);204 Mockito.verifyNoMoreInteractions(model);205 }206 207 @Test208 public void createAccountReturnValueWhenBindingResultHasNoErrorsUserDoesNotExistsUsernamesMatchAndPasswordsDoNotMatch() {209 Mockito.when(result.hasErrors()).thenReturn(false);210 211 userDto.setUserName("test@fake.com");212 userDto.setReUserName("test@fake.com");213 userDto.setUserPass("pass");214 userDto.setReUserPass("nopass");215 216 final String view = accessController.createAccount(userDto, result, request, model);217 218 assertEquals(view, "access/signup");219 220 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();221 Mockito.verify(model, VerificationModeFactory.times(1)).addAttribute("errorMessage", "signup.invalid.password.notmatching");222 Mockito.verifyNoMoreInteractions(result);223 Mockito.verifyNoMoreInteractions(model);224 }225 226 @Test227 public void createAccountReturnValueWhenBindingResultHasNoErrorsUserDoesNotExistsUsernamesMatchPasswordsMatch() throws ConstraintViolationException, Exception {228 Mockito.when(result.hasErrors()).thenReturn(false);229 Mockito.when(userService.addUser(Mockito.any(User.class))).thenReturn(false);230 231 userDto.setUserName("test@fake.com");232 userDto.setReUserName("test@fake.com");233 userDto.setUserPass("pass");234 userDto.setReUserPass("pass");235 236 final String view = accessController.createAccount(userDto, result, request, model);237 238 assertEquals(view, "access/signup");239 240 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();241 /**242 * I think the reason VerificationModeFactory is called 3 times is due243 * to the fact that there are 3 if tests before the user is added.244 */245 Mockito.verify(userService, VerificationModeFactory.times(3)).addUser(Mockito.any(User.class));246 Mockito.verify(model, VerificationModeFactory.times(2)).addAttribute(Mockito.anyString(), Mockito.any(User.class));247 Mockito.verify(model).addAttribute("errorMessage", "signup.invalid.username.duplicate");248 Mockito.verifyNoMoreInteractions(result);249 Mockito.verifyNoMoreInteractions(model);250 }251 252 @Test253 public void createAccountReturnValueWhenBindingResultHasNoErrorsUserDoesNotExistsUsernamesMatchPasswordsMatchAndIsSuccessful() throws ConstraintViolationException, Exception {254 Mockito.when(result.hasErrors()).thenReturn(false);255 Mockito.when(userService.addUser(Mockito.any(User.class))).thenReturn(true);256 Mockito.when(request.getSession()).thenReturn(session);257 Mockito.doNothing().when(session).setAttribute(Mockito.anyString(), Mockito.any());258 259 userDto.setUserName("test@fake.com");260 userDto.setReUserName("test@fake.com");261 userDto.setUserPass("pass");262 userDto.setReUserPass("pass");263 264 final String view = accessController.createAccount(userDto, result, request, model);265 266 assertEquals(view, "index");267 268 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();269 Mockito.verify(userService, VerificationModeFactory.times(2)).addUser(Mockito.any(User.class));270 Mockito.verify(model, VerificationModeFactory.times(3)).addAttribute(Mockito.anyString(), Mockito.any(User.class));271 Mockito.verify(model).addAttribute("status", "User was successfully added.");272 Mockito.verify(model).addAttribute("message", "Thank you for your registration.");273 Mockito.verifyNoMoreInteractions(result);274 Mockito.verifyNoMoreInteractions(userService);275 Mockito.verifyNoMoreInteractions(model);276 }277 278 @Test279 public void createAccountReturnValueWhenBindingResultHasNoErrorsUserDoesNotExistsUsernamesMatchPasswordsMatchAndThrowsException() throws ConstraintViolationException, Exception {280 Mockito.when(result.hasErrors()).thenReturn(false);281 Mockito.when(userService.addUser(Mockito.any(User.class))).thenReturn(true);282 283 userDto.setUserName("test@fake.com");284 userDto.setReUserName("test@fake.com");285 userDto.setUserPass("pass");286 userDto.setReUserPass("pass");287 288 final String view = accessController.createAccount(userDto, result, request, model);289 290 assertEquals(view, "access/signup");291 292 Mockito.verify(result, VerificationModeFactory.times(1)).hasErrors();293 Mockito.verify(userService, VerificationModeFactory.times(1)).addUser(Mockito.any(User.class));294 Mockito.verify(model, VerificationModeFactory.times(4)).addAttribute(Mockito.anyString(), Mockito.any(User.class));295 Mockito.verify(model).addAttribute("status", "User was successfully added.");296 Mockito.verify(model).addAttribute("message", "Thank you for your registration.");297 Mockito.verify(model).addAttribute("errorMessage", "could.not.create.new.user");298 Mockito.verifyNoMoreInteractions(result);299 Mockito.verifyNoMoreInteractions(userService);300 Mockito.verifyNoMoreInteractions(model);301 }302}
...
Source:PushControllerTest.java
...62 controller.post(request, response);63 64 verify(response).setStatus(204);65 verify(pushSubscriberManager).subscribe(TOPIC, CALLBACK, null, PushSubscriber.DEFAULT_LEASE_SECONDS, null, null, true, true);66 Mockito.verifyNoMoreInteractions(response);67 }68 @Test69 public void subscribeWithLeaseSeconds() throws Exception {70 when(request.getParameter("hub.mode")).thenReturn("subscribe");71 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());72 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());73 when(request.getParameter("hub.verify")).thenReturn("sync");74 when(request.getParameter("hub.lease_seconds")).thenReturn("123");75 76 controller.post(request, response);77 78 verify(response).setStatus(204);79 verify(pushSubscriberManager).subscribe(TOPIC, CALLBACK, null, 123, null, null, true, true);80 Mockito.verifyNoMoreInteractions(response);81 }82 @Test83 public void subscribeWithInvalidLeaseSeconds() throws Exception {84 when(request.getParameter("hub.mode")).thenReturn("subscribe");85 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());86 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());87 when(request.getParameter("hub.verify")).thenReturn("sync");88 when(request.getParameter("hub.lease_seconds")).thenReturn("foo");89 90 controller.post(request, response);91 92 verify(response).sendError(Mockito.eq(500), Mockito.anyString());93 Mockito.verifyZeroInteractions(pushSubscriberManager);94 Mockito.verifyNoMoreInteractions(response);95 }96 97 @Test98 public void subscribeWithLeaseSecondsAndToken() throws Exception {99 when(request.getParameter("hub.mode")).thenReturn("subscribe");100 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());101 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());102 when(request.getParameter("hub.verify")).thenReturn("sync");103 when(request.getParameter("hub.lease_seconds")).thenReturn("123");104 when(request.getParameter("hub.verify_token")).thenReturn("vt");105 106 controller.post(request, response);107 108 verify(response).setStatus(204);109 verify(pushSubscriberManager).subscribe(TOPIC, CALLBACK, null, 123, "vt", null, true, true);110 Mockito.verifyNoMoreInteractions(response);111 }112 113 @Test114 public void subscribeWithSecret() throws Exception {115 when(request.getParameter("hub.mode")).thenReturn("subscribe");116 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());117 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());118 when(request.getParameter("hub.verify")).thenReturn("sync");119 when(request.getParameter("hub.lease_seconds")).thenReturn("123");120 when(request.getParameter("hub.secret")).thenReturn(SECRET);121 122 controller.post(request, response);123 124 verify(response).setStatus(204);125 verify(pushSubscriberManager).subscribe(TOPIC, CALLBACK, null, 123, null, SECRET, true, true);126 Mockito.verifyNoMoreInteractions(response);127 }128 129 @Test130 public void subscribeWithoutVerify() throws Exception {131 when(request.getParameter("hub.mode")).thenReturn("subscribe");132 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());133 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());134 135 controller.post(request, response);136 137 verify(response).sendError(Mockito.eq(500), Mockito.anyString());138 Mockito.verifyZeroInteractions(pushSubscriberManager);139 Mockito.verifyNoMoreInteractions(response);140 }141 142 @Test143 public void subscribeWithoutCallback() throws Exception {144 when(request.getParameter("hub.mode")).thenReturn("subscribe");145 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());146 when(request.getParameter("hub.verify")).thenReturn("sync");147 148 controller.post(request, response);149 150 verify(response).sendError(Mockito.eq(500), Mockito.anyString());151 Mockito.verifyZeroInteractions(pushSubscriberManager);152 Mockito.verifyNoMoreInteractions(response);153 }154 155 @Test156 public void subscribeWithoutTopic() throws Exception {157 when(request.getParameter("hub.mode")).thenReturn("subscribe");158 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());159 when(request.getParameter("hub.verify")).thenReturn("sync");160 161 controller.post(request, response);162 163 verify(response).sendError(Mockito.eq(500), Mockito.anyString());164 Mockito.verifyZeroInteractions(pushSubscriberManager);165 Mockito.verifyNoMoreInteractions(response);166 }167 @Test168 public void unsubscribe() throws Exception {169 when(request.getParameter("hub.mode")).thenReturn("unsubscribe");170 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());171 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());172 when(request.getParameter("hub.verify")).thenReturn("sync");173 174 controller.post(request, response);175 176 verify(response).setStatus(204);177 verify(pushSubscriberManager).unsubscribe(TOPIC, CALLBACK, true);178 Mockito.verifyNoMoreInteractions(response);179 }180 @Test181 public void unsubscribeWithoutCallback() throws Exception {182 when(request.getParameter("hub.mode")).thenReturn("unsubscribe");183 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());184 when(request.getParameter("hub.verify")).thenReturn("sync");185 186 controller.post(request, response);187 188 verify(response).sendError(Mockito.eq(500), Mockito.anyString());189 Mockito.verifyZeroInteractions(pushSubscriberManager);190 Mockito.verifyNoMoreInteractions(response);191 }192 193 @Test194 public void unsubscribeWithoutTopic() throws Exception {195 when(request.getParameter("hub.mode")).thenReturn("unsubscribe");196 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());197 when(request.getParameter("hub.verify")).thenReturn("sync");198 199 controller.post(request, response);200 201 verify(response).sendError(Mockito.eq(500), Mockito.anyString());202 Mockito.verifyZeroInteractions(pushSubscriberManager);203 Mockito.verifyNoMoreInteractions(response);204 }205 206 @Test207 public void unsubscribeWithoutVerify() throws Exception {208 when(request.getParameter("hub.mode")).thenReturn("unsubscribe");209 when(request.getParameter("hub.callback")).thenReturn(CALLBACK.toString());210 when(request.getParameter("hub.topic")).thenReturn(TOPIC.toString());211 212 controller.post(request, response);213 214 verify(response).sendError(Mockito.eq(500), Mockito.anyString());215 Mockito.verifyZeroInteractions(pushSubscriberManager);216 Mockito.verifyNoMoreInteractions(response);217 }218 219 @Test220 public void publish() throws Exception {221 when(request.getParameter("hub.mode")).thenReturn("publish");222 when(request.getParameterValues("hub.url")).thenReturn(new String[]{TOPIC.toString()});223 224 controller.post(request, response);225 226 verify(response).setStatus(204);227 verify(pushSubscriberManager).retrive(TOPIC);228 Mockito.verifyNoMoreInteractions(response);229 Mockito.verifyNoMoreInteractions(pushSubscriberManager);230 }231 @Test232 public void publishMultipleUrls() throws Exception {233 URI otherTopic = URI.create("http://example.com/other");234 235 when(request.getParameter("hub.mode")).thenReturn("publish");236 when(request.getParameterValues("hub.url")).thenReturn(new String[]{TOPIC.toString(), otherTopic.toString()});237 238 controller.post(request, response);239 240 verify(response).setStatus(204);241 verify(pushSubscriberManager).retrive(TOPIC);242 verify(pushSubscriberManager).retrive(otherTopic);243 Mockito.verifyNoMoreInteractions(response);244 Mockito.verifyNoMoreInteractions(pushSubscriberManager);245 }246 247 @Test248 public void publishWithoutUrl() throws Exception {249 when(request.getParameter("hub.mode")).thenReturn("publish");250 251 controller.post(request, response);252 253 verify(response).sendError(Mockito.eq(500), Mockito.anyString());254 Mockito.verifyZeroInteractions(pushSubscriberManager);255 Mockito.verifyNoMoreInteractions(response);256 }257 258 @Test259 public void publishInvalidUrlSceheme() throws Exception {260 when(request.getParameter("hub.mode")).thenReturn("publish");261 when(request.getParameterValues("hub.url")).thenReturn(new String[]{"foo://dummy"});262 263 controller.post(request, response);264 265 verify(response).sendError(Mockito.eq(500));266 Mockito.verifyZeroInteractions(pushSubscriberManager);267 Mockito.verifyNoMoreInteractions(response);268 }269 270 271 @Test272 public void withoutMode() throws Exception {273 controller.post(request, response);274 275 verify(response).sendError(Mockito.eq(500), Mockito.anyString());276 Mockito.verifyZeroInteractions(pushSubscriberManager);277 Mockito.verifyNoMoreInteractions(response);278 }279 280 @Test281 public void invalidMode() throws Exception {282 when(request.getParameter("hub.mode")).thenReturn("foo");283 284 controller.post(request, response);285 286 verify(response).sendError(Mockito.eq(500), Mockito.anyString());287 Mockito.verifyZeroInteractions(pushSubscriberManager);288 Mockito.verifyNoMoreInteractions(response);289 }290 291}...
Source:IndividualMouseActionsTest.java
...39 action.perform();40 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);41 order.verify(mockMouse).mouseMove(mockCoordinates);42 order.verify(mockMouse).mouseDown(mockCoordinates);43 order.verifyNoMoreInteractions();44 }45 @Test46 public void mouseClickAndHoldActionOnCurrentLocation() {47 ClickAndHoldAction action = new ClickAndHoldAction(mockMouse, null);48 action.perform();49 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);50 order.verify(mockMouse).mouseDown(null);51 order.verifyNoMoreInteractions();52 }53 @Test54 public void mouseReleaseAction() {55 ButtonReleaseAction action = new ButtonReleaseAction(mockMouse, locatableStub);56 action.perform();57 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);58 order.verify(mockMouse).mouseMove(mockCoordinates);59 order.verify(mockMouse).mouseUp(mockCoordinates);60 order.verifyNoMoreInteractions();61 }62 @Test63 public void mouseReleaseActionOnCurrentLocation() {64 ButtonReleaseAction action = new ButtonReleaseAction(mockMouse, null);65 action.perform();66 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);67 order.verify(mockMouse).mouseUp(null);68 order.verifyNoMoreInteractions();69 }70 @Test71 public void mouseClickAction() {72 ClickAction action = new ClickAction(mockMouse, locatableStub);73 action.perform();74 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);75 order.verify(mockMouse).mouseMove(mockCoordinates);76 order.verify(mockMouse).click(mockCoordinates);77 order.verifyNoMoreInteractions();78 }79 @Test80 public void mouseClickActionOnCurrentLocation() {81 ClickAction action = new ClickAction(mockMouse, null);82 action.perform();83 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);84 order.verify(mockMouse).click(null);85 order.verifyNoMoreInteractions();86 }87 @Test88 public void mouseDoubleClickAction() {89 DoubleClickAction action = new DoubleClickAction(mockMouse, locatableStub);90 action.perform();91 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);92 order.verify(mockMouse).mouseMove(mockCoordinates);93 order.verify(mockMouse).doubleClick(mockCoordinates);94 order.verifyNoMoreInteractions();95 }96 @Test97 public void mouseDoubleClickActionOnCurrentLocation() {98 DoubleClickAction action = new DoubleClickAction(mockMouse, null);99 action.perform();100 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);101 order.verify(mockMouse).doubleClick(null);102 order.verifyNoMoreInteractions();103 }104 @Test105 public void mouseMoveAction() {106 MoveMouseAction action = new MoveMouseAction(mockMouse, locatableStub);107 action.perform();108 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);109 order.verify(mockMouse).mouseMove(mockCoordinates);110 order.verifyNoMoreInteractions();111 }112 @Test113 public void mouseMoveActionToCoordinatesInElement() {114 MoveToOffsetAction action = new MoveToOffsetAction(mockMouse, locatableStub, 20, 20);115 action.perform();116 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);117 order.verify(mockMouse).mouseMove(mockCoordinates, 20, 20);118 order.verifyNoMoreInteractions();119 }120 @Test121 public void mouseContextClickAction() {122 ContextClickAction action = new ContextClickAction(mockMouse, locatableStub);123 action.perform();124 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);125 order.verify(mockMouse).mouseMove(mockCoordinates);126 order.verify(mockMouse).contextClick(mockCoordinates);127 order.verifyNoMoreInteractions();128 }129 @Test130 public void mouseContextClickActionOnCurrentLocation() {131 ContextClickAction action = new ContextClickAction(mockMouse, null);132 action.perform();133 InOrder order = Mockito.inOrder(mockMouse, mockCoordinates);134 order.verify(mockMouse).contextClick(null);135 order.verifyNoMoreInteractions();136 }137}...
Source:VerifyNoMoreInteractions.java
...22 * Verifies no more interactions, delegates to Mockito if PowerMockito doesn't23 * find a supplied mock.24 */25public class VerifyNoMoreInteractions {26 public static void verifyNoMoreInteractions(Object... objects) {27 for (Object mock : objects) {28 if (mock instanceof Class<?>) {29 verifyNoMoreInteractions((Class<?>) mock);30 } else {31 MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository32 .getInstanceMethodInvocationControl(mock);33 if (invocationControl != null) {34 invocationControl.verifyNoMoreInteractions();35 } else {36 /*37 * Delegate to Mockito if we have no handler registered for38 * this object.39 */40 Mockito.verifyNoMoreInteractions(mock);41 }42 }43 }44 }45 private static void verifyNoMoreInteractions(Class<?>... types) {46 for (Class<?> type : types) {47 final MockitoMethodInvocationControl invocationHandler = (MockitoMethodInvocationControl) MockRepository48 .getStaticMethodInvocationControl(type);49 if (invocationHandler != null) {50 invocationHandler.verifyNoMoreInteractions();51 }52 MockitoNewInvocationControl<?> newInvocationControl = (MockitoNewInvocationControl<?>) MockRepository.getNewInstanceControl(type);53 if (newInvocationControl != null) {54 newInvocationControl.verifyNoMoreInteractions();55 }56 }57 }58}...
verifyNoMoreInteractions
Using AI Code Generation
1package org.kodejava.example.mockito;2import org.junit.Test;3import org.junit.runner.RunWith;4import org.mockito.Mock;5import org.mockito.runners.MockitoJUnitRunner;6import java.util.List;7import static org.mockito.Mockito.verifyNoMoreInteractions;8@RunWith(MockitoJUnitRunner.class)9public class VerifyNoMoreInteractionsTest {10 private List mockList;11 public void testVerifyNoMoreInteractions() {12 mockList.add("one");13 mockList.clear();14 verifyNoMoreInteractions(mockList);15 }16}
verifyNoMoreInteractions
Using AI Code Generation
1import org.junit.Test;2import org.junit.runner.RunWith;3import org.mockito.Mock;4import org.mockito.junit.MockitoJUnitRunner;5import java.util.List;6import static org.mockito.Mockito.verifyNoMoreInteractions;7@RunWith(MockitoJUnitRunner.class)8public class 1 {9 private List<String> mockList;10 public void test() {11 mockList.add("one");12 verifyNoMoreInteractions(mockList);13 }14}15-> at 1.test(1.java:17)16-> at 1.test(1.java:16)17 list.add(eq("one"))18 list.add("one")19 verify(mock).someMethod(eq(1));20 verify(mock).someMethod(anyInt());21 verify(mock, times(2)).someMethod(anyInt());22 verify(mock, atLeastOnce()).someMethod(anyInt());23 verify(mock, atLeast(2)).someMethod(anyInt());24 verify(mock, atMost(5)).someMethod(anyInt());25 verify(mock, never()).someMethod(anyInt());26 verify(mock).someMethod(anyInt());27 verify(mock).someMethod(1);28 verify(mock, times(2)).someMethod(1);29 verify(mock, atLeastOnce()).someMethod(1);30 verify(mock, atLeast(2)).someMethod(1);31 verify(mock, atMost(5)).someMethod(1);32 verify(mock, never()).someMethod(1);33-> at 1.test(1.java:16)34Related posts: Mockito verify static method Mockito verify method Mockito verifyNoMoreInteractions() method Mockito verifyNoInteractions() method Mockito verify() method Mockito verifyZeroInteractions() method Mockito verify() method Mockito verify() method Mockito verify() method Mockito verify() method
verifyNoMoreInteractions
Using AI Code Generation
1import static org.mockito.Mockito.*;2public class 1 {3 public static void main(String[] args) {4 List mockedList = mock(List.class);5 mockedList.add("one");6 mockedList.clear();7 verify(mockedList).add("one");8 verifyNoMoreInteractions(mockedList);9 }10}11-> at 1.main(1.java:11)123. verifyZeroInteractions()13import static org.mockito.Mockito.*;14public class 1 {15 public static void main(String[] args) {16 List mockedList = mock(List.class);17 verifyZeroInteractions(mockedList);18 }19}20-> at 1.main(1.java:11)214. verifyNoMoreInteractions()22import static org.mockito.Mockito.*;23public class 1 {24 public static void main(String[] args) {25 List mockedList = mock(List.class);26 mockedList.add("one");27 mockedList.clear();28 verify(mockedList).add("one");29 verifyNoMoreInteractions(mockedList);30 }31}32-> at 1.main(1.java:11)335. verifyNoInteractions()34import static org.mockito.Mockito.*;35public class 1 {36 public static void main(String[] args) {37 List mockedList = mock(List.class);38 verifyNoInteractions(mockedList);39 }40}41-> at 1.main(1.java:11)426. verifyNoMoreInteractions()
verifyNoMoreInteractions
Using AI Code Generation
1import org.mockito.Mockito;2import org.mockito.InOrder;3import org.mockito.ArgumentCaptor;4import org.mockito.stubbing.OngoingStubbing;5import org.mockito.invocation.InvocationOnMock;6import org.mockito.stubbing.Answer;7import org.mockito.verification.VerificationMode;8import org.mockito.verification.VerificationWithTimeout;9import org.mockito.verification.VerificationAfterDelay;10import org.mockito.verification.VerificationStrategy;11import org.mockito.exceptions.verification.NeverWantedButInvoked;12import org.mockito.exceptions.verification.NoInteractionsWanted;13import org.mockito.exceptions.verification.TooLittleActualInvocations;14import org.mockito.exceptions.verification.TooManyActualInvocations;15import org.mockito.exceptions.verification.TooManyActualInvocations;16import org.mockito.exceptions.verification.VerificationInOrderFailure;17import org.mockito.exceptions.verification.WantedButNotInvoked;18import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;19import org.mockito.exceptions.verification.junit.NoInteractionsWanted;20import org.mockito.exceptions.verification.junit.TooLittleActualInvocations;21import org.mockito.exceptions.verification.junit.TooManyActualInvocations;22import org.mockito.exceptions.verification.junit.WantedButNotInvoked;23import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;24import org.mockito.exceptions.verification.junit.NoInteractionsWanted;25import org.mockito.exceptions.verification.junit.TooLittleActualInvocations;26import org.mockito.exceptions.verification.junit.TooManyActualInvocations;27import org.mockito.exceptions.verification.junit.WantedButNotInvoked;28import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;29import org.mockito.exceptions.verification.junit.NoInteractionsWanted;30import org.mockito.exceptions.verification.junit.TooLittleActualInvocations;31import org.mockito.exceptions.verification.junit.TooManyActualInvocations;32import org.mockito.exceptions.verification.junit.WantedButNotInvoked;33import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;34import org.mockito.exceptions.verification.junit.NoInteractionsWanted;35import org.mockito.exceptions.verification.junit.TooLittleActualInvocations;36import org.mockito.exceptions.verification.junit.TooManyActualInvocations;37import org.mockito.exceptions.verification.junit.WantedButNotInvoked;38import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;39import org.mockito.exceptions.verification.junit.NoInteractionsWanted;40import org.mockito.exceptions.verification.junit.TooLittleActualInvocations;41import org.mockito.exceptions.verification.junit.TooManyActualInvocations;42import org.mockito.exceptions.verification.junit.Wanted
verifyNoMoreInteractions
Using AI Code Generation
1package com.ack.testing.mocking.mockito.verify;2import org.junit.Test;3import java.util.List;4import static org.mockito.Mockito.*;5public class VerifyNoMoreInteractionsTest {6 public void testVerifyNoMoreInteractions() {7 List mockedList = mock( List.class );8 mockedList.add( "one" );9 mockedList.clear();10 verify( mockedList ).add( "one" );11 verify( mockedList ).clear();12 verifyNoMoreInteractions( mockedList );13 }14}15-> at com.ack.testing.mocking.mockito.verify.VerifyNoMoreInteractionsTest.testVerifyNoMoreInteractions(VerifyNoMoreInteractionsTest.java:21)16Mockito.verifyNoMoreInteractions() Method17public static void verifyNoMoreInteractions(Object... mocks)18package com.ack.testing.mocking.mockito.verify;19import org.junit.Test;20import java.util.List;21import static org.mockito.Mockito.*;22public class VerifyNoMoreInteractionsTest {23 public void testVerifyNoMoreInteractions() {
verifyNoMoreInteractions
Using AI Code Generation
1package com.ack.mockito.verify;2import org.junit.Test;3import org.mockito.Mockito;4import java.util.LinkedList;5import java.util.List;6public class VerifyNoMoreInteractionsTest {7 public void verifyNoMoreInteractionsTest() {8 List mockedList = Mockito.mock(LinkedList.class);9 mockedList.add("one");10 mockedList.clear();11 Mockito.verify(mockedList).add("one");12 Mockito.verify(mockedList).clear();13 Mockito.verifyNoMoreInteractions(mockedList);14 }15}16-> at VerifyNoMoreInteractionsTest.verifyNoMoreInteractionsTest(VerifyNoMoreInteractionsTest.java:21)
verifyNoMoreInteractions
Using AI Code Generation
1package mockito;2import java.util.LinkedList;3import java.util.List;4import org.junit.Test;5import static org.mockito.Mockito.*;6public class VerifyNoMoreInteractionsTest {7 public void testVerifyNoMoreInteractions() {8 List mockedList = mock(LinkedList.class);9 mockedList.add("one");10 mockedList.clear();11 verify(mockedList).add("one");12 verify(mockedList).clear();13 verifyNoMoreInteractions(mockedList);14 }15}16-> at mockito.VerifyNoMoreInteractionsTest.testVerifyNoMoreInteractions(VerifyNoMoreInteractionsTest.java:21)17Related posts: Mockito – verifyNoMoreInteractions() method Mockito – verifyZeroInteractions() method Mockito – verify() method Mockito – verifyNoMoreInte
verifyNoMoreInteractions
Using AI Code Generation
1import org.junit.Test;2import org.mockito.Mockito;3import java.util.List;4import static org.mockito.Mockito.*;5public class MockitoVerifyNoMoreInteractions {6 public void verifyNoMoreInteractionsTest() {7 List list = mock(List.class);8 list.add("one");9 list.clear();10 verify(list).add("one");11 verify(list).clear();12 verifyNoMoreInteractions(list);13 }14}
verifyNoMoreInteractions
Using AI Code Generation
1import static org.mockito.Mockito.*;2import org.mockito.Mockito;3import org.mockito.InOrder;4import org.mockito.exceptions.verification.NoInteractionsWanted;5import org.mockito.exceptions.base.MockitoAssertionError;6import org.mockito.exceptions.VerificationInOrderFailure;7import org.mockito.exceptions.base.MockitoException;8import org.mockito.exceptions.verification.NeverWantedButInvoked;9import org.mockito.exceptions.verification.TooLittleActualInvocations;10import org.mockito.exceptions.verification.VerificationError;11import org.mockito.exceptions.verification.VerificationInOrderFailure;12import org.mockito.exceptions.verification.WantedButNotInvoked;13import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;14import org.mockito.exceptions.verification.junit.JUnitFailure;15import org.mockito.exceptions.verification.junit.JUnitNoInteractionsWanted;16import org.mockito.exceptions.verification.junit.JUnitNoInteractionsWanted;17import org.mockito.exceptions.verification.junit.JUnitNoMoreInteractionsWanted;18import org.mockito.exceptions.verification.junit.JUnitNoMoreInteractionsWanted;19import org.mockito.exceptions.verification.junit.JUnitNoMoreInteractionsWanted;20import org.mockito.exceptions.verification.junit.JUnitTooLittleActualInvocations;21import org.mockito.exceptions.verification.junit.JUnitTooLittleActualInvocations;22import or
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!