### Question:
AdminUserValidator { public void validateAdminUserOnAdd(AdminUser user) throws InvalidDtoException { if (user == null) { throw new InvalidDtoException("Input user undefined"); } if (user.getId() == null) { throw new InvalidDtoException("ID of user is undefined"); } Optional<AdminUser> optionalAdmin = this.adminUserRepository.findById(user.getId()); if (optionalAdmin.isPresent()) { throw new InvalidDtoException("User is already registered as admin in databank"); } checkIdForSemanticExceptions(user.getId()); } @Autowired  AdminUserValidator(UserService userService, AdminUserRepository adminUserRepository); void validateAdminUserOnAdd(AdminUser user); void validateAdminUserId(UUID userId);  }### Answer:
@Test public void validateAdminUserOnAdd_userIsNull_expectedThrow() { try { adminUserValidator.validateAdminUserOnAdd(null); fail("Should throw InvalidDtoException."); } catch (Exception ex) { assertThat( "Should only throw InvalidDtoException.", ex, instanceOf(InvalidDtoException.class)); } verify(userService, never()).getCurrentUser(); verify(userService, never()).doesUserExist(UUID.randomUUID()); }
@Test public void validateAdminUserOnAdd_existingAdmin_expectedThrow() throws InvalidDtoException { when(userToInsert.getId()).thenReturn(existingAdminId); adminUser.setId(userToInsert.getId()); try { adminUserValidator.validateAdminUserOnAdd(adminUser); fail("Should throw InvalidDtoException."); } catch (Exception ex) { assertThat( "Should only throw InvalidDtoException.", ex, instanceOf(InvalidDtoException.class)); } verify(adminUserRepository).findById(existingAdminId); verify(userService, never()).getCurrentUser(); verify(userService, never()).doesUserExist(existingAdminId); }
@Test public void validateAdminUserOnAdd_existingAdmin_expectedNoThrow() { adminUser.setId(userToInsert.getId()); try { adminUserValidator.validateAdminUserOnAdd(adminUser); } catch (Exception ex) { assertThat("Should not throw any Exception.", ex, instanceOf(Exception.class)); } }