### Question:
UserDeviceServiceImpl implements UserDeviceService { @Transactional @Nonnull @Override public UserDevice createUserDevice(@Nonnull final Long userId, @Nonnull final UserDeviceDto userDeviceDto) { assertUserId(userId); assertUserDeviceDto(userDeviceDto); LOGGER.debug("Creating user device for user with id - {}, user device DTO - {}", userId, userDeviceDto); final User user = userService.getUserById(userId); assertNoUserDeviceExistsForUuId(user, userDeviceDto.getUuId()); UserDevice userDevice = new UserDevice(); userDevice.setUser(user); userDeviceDto.updateDomainEntityProperties(userDevice); userDevice = userDeviceRepository.save(userDevice); LOGGER.debug("Successfully created user device with id - {}, user device - {}", userDevice.getId(), userDevice); return userDevice; } UserDeviceServiceImpl(); @Nonnull @Override UserDevice getUserDeviceById(@Nonnull final Long userDeviceId); @Transactional @Nonnull @Override UserDevice createUserDevice(@Nonnull final Long userId, @Nonnull final UserDeviceDto userDeviceDto); @Nonnull @Override UserDevice getUserDeviceByUuId(@Nonnull final Long userId, @Nonnull final String uuId); @Nonnull @Override boolean checkIfUserDeviceExistsForUuId(@Nonnull final Long userId, @Nonnull final String uuId); @Transactional @Nonnull @Override UserDevice getOrCreateUserDevice(@Nonnull final Long userId, @Nonnull final UserDeviceDto userDeviceDto); UserDeviceRepository getUserDeviceRepository(); void setUserDeviceRepository(final UserDeviceRepository userDeviceRepository);  }### Answer:
@Test public void testCreateUserDeviceWithInvalidArguments() { final Long userId = 1L; final UserDeviceDto deviceDto = getServicesImplTestHelper().createUserDeviceDto(); resetAll(); replayAll(); try { userDeviceService.createUserDevice(null, deviceDto); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } try { userDeviceService.createUserDevice(userId, null); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } try { userDeviceService.createUserDevice(userId, new UserDeviceDto(null, deviceDto.getOsType())); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } try { userDeviceService.createUserDevice(userId, new UserDeviceDto(deviceDto.getUuId(), null)); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } verifyAll(); }
@Test public void testCreateUserDeviceWithExistingUuId() { final Long userId = 1L; final User user = getServicesImplTestHelper().createUser(); user.setId(userId); final UserDeviceDto deviceDto = getServicesImplTestHelper().createUserDeviceDto(); final Long existingDeviceId = 2L; final UserDevice existingDevice = getServicesImplTestHelper().createUserDevice(); existingDevice.setId(existingDeviceId); resetAll(); expect(userService.getUserById(eq(userId))).andReturn(user).once(); expect(userDeviceRepository.findByUserAndUuId(eq(user), eq(deviceDto.getUuId()))).andReturn(existingDevice).once(); replayAll(); try { userDeviceService.createUserDevice(userId, deviceDto); fail("Exception should be thrown"); } catch (final UserDeviceAlreadyExistsException ex) { assertUserDeviceAlreadyExistsException(ex, userId, deviceDto.getUuId(), existingDeviceId); } verifyAll(); }
@Test public void testCreateUserDevice() { final Long userId = 1L; final User user = getServicesImplTestHelper().createUser(); user.setId(userId); final UserDeviceDto deviceDto = getServicesImplTestHelper().createUserDeviceDto(); resetAll(); expect(userService.getUserById(eq(userId))).andReturn(user).once(); expect(userDeviceRepository.findByUserAndUuId(eq(user), eq(deviceDto.getUuId()))).andReturn(null).once(); expect(userDeviceRepository.save(isA(UserDevice.class))).andAnswer(() -> (UserDevice) getCurrentArguments()[0]).once(); replayAll(); final UserDevice result = userDeviceService.createUserDevice(userId, deviceDto); getServicesImplTestHelper().assertUserDevice(result, deviceDto); Assert.assertEquals(user, result.getUser()); verifyAll(); }