### Question:
UserDeviceServiceImpl implements UserDeviceService { @Transactional @Nonnull @Override public UserDevice getOrCreateUserDevice(@Nonnull final Long userId, @Nonnull final UserDeviceDto userDeviceDto) { assertUserId(userId); assertUserDeviceDto(userDeviceDto); LOGGER.debug("Getting or creating user device for user id - {}, device DTO - {}", userId, userDeviceDto); final User user = userService.getUserById(userId); UserDevice userDevice = userDeviceRepository.findByUserAndUuId(user, userDeviceDto.getUuId()); if (userDevice == null) { userDevice = createUserDevice(userId, userDeviceDto); LOGGER.debug("Created user device with id - {} for user with id - {}, device DTO - {}", userDevice.getId(), user.getId(), userDeviceDto); } else { LOGGER.debug("User device with id - {} already exists for user with id - {}, device DTO - {}", userDevice.getId(), user.getId(), userDeviceDto); } 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 testGetOrCreateUserDeviceWithInvalidArguments() { final Long userId = 1L; final UserDeviceDto deviceDto = getServicesImplTestHelper().createUserDeviceDto(); resetAll(); replayAll(); try { userDeviceService.getOrCreateUserDevice(null, deviceDto); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } try { userDeviceService.getOrCreateUserDevice(userId, new UserDeviceDto(null, deviceDto.getOsType())); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } try { userDeviceService.getOrCreateUserDevice(userId, new UserDeviceDto(deviceDto.getUuId(), null)); fail("Exception should be thrown"); } catch (final IllegalArgumentException ex) { } verifyAll(); }
@Test public void testGetOrCreateUserDeviceWhenItDoesNotExist() { 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).times(2); expect(userDeviceRepository.findByUserAndUuId(eq(user), eq(deviceDto.getUuId()))).andReturn(null).times(2); expect(userDeviceRepository.save(isA(UserDevice.class))).andAnswer(() -> (UserDevice) getCurrentArguments()[0]).once(); replayAll(); final UserDevice result = userDeviceService.getOrCreateUserDevice(userId, deviceDto); getServicesImplTestHelper().assertUserDevice(result, deviceDto); verifyAll(); }