### Question:
UserSettingsService { public UserSettings getUserSettingsByUser(User user) { UserSettings userSettings = userSettingsRepository.findUserSettingsByUserId(user.getId()); if (userSettings == null) { userSettings = new UserSettings(); userSettings.setUserId(user.getId()); userSettings.setDefaultOkrCompany(getDefaultCompany()); userSettings.setDefaultTeam(getDefaultTeam(user, userSettings.getDefaultOkrCompany())); userSettings = userSettingsRepository.save(userSettings); activityService.createActivity(user, userSettings, Action.CREATED); logger.info( "Created User Settings " + userSettings.getName() + "(id: " + userSettings.getId() + ") "); } return userSettings; } @Autowired  UserSettingsService(
      UserSettingsRepository userSettingsRepository,
      ActivityService activityService,
      CompanyService companyService); UserSettings getUserSettingsByUser(User user); UserSettings updateUserSettings(UserSettings userSettings, User user);  }### Answer:
@Test public void getUserSettingsByUser_expectNewSettingsBeingCreatedIfThereNoSuchUsersUserSettings() { whenUserSettingsSaveReturnSameValue(); UserSettings userSettings = this.userSettingsService.getUserSettingsByUser(user); Assert.assertEquals(userId, userSettings.getUserId()); verify(userSettingsRepository, times(1)).save(any()); }
@Test public void getUserSettingsByUser_expectNoCompanyIfThereIsMoreThanOneCompany() { when(userSettingsRepository.findUserSettingsByUserId(userId)).thenReturn(null); whenUserSettingsSaveReturnSameValue(); OkrCompany okrCompany = new OkrCompany(); okrCompany.setCycle(new Cycle()); okrCompany.getCycle().setCycleState(CycleState.ACTIVE); OkrCompany okrCompany1 = new OkrCompany(); okrCompany1.setCycle(new Cycle()); okrCompany1.getCycle().setCycleState(CycleState.ACTIVE); when(companyService.getAllCompanies()).thenReturn(Arrays.asList(okrCompany, okrCompany1)); UserSettings userSettings = this.userSettingsService.getUserSettingsByUser(user); Assert.assertNull(userSettings.getDefaultOkrCompany()); verify(userSettingsRepository, times(1)).save(any()); verify(companyService, times(1)).getAllCompanies(); }
@Test public void getUserSettingsByUser_expectCompanyIsSetIfThereIsOnlyOneCompany() { when(userSettingsRepository.findUserSettingsByUserId(userId)).thenReturn(null); OkrCompany okrCompany = new OkrCompany(); okrCompany.setCycle(new Cycle()); okrCompany.getCycle().setCycleState(CycleState.ACTIVE); when(companyService.getAllCompanies()).thenReturn(Collections.singletonList(okrCompany)); whenUserSettingsSaveReturnSameValue(); UserSettings userSettings = this.userSettingsService.getUserSettingsByUser(user); Assert.assertEquals(okrCompany, userSettings.getDefaultOkrCompany()); verify(userSettingsRepository, times(1)).save(any()); verify(companyService, times(1)).getAllCompanies(); }
@Test public void getUserSettingsByUser_expectUserSettingsByUser() { UserSettings storedUserSettings = new UserSettings(); Long userSettingsId = 40L; storedUserSettings.setUserId(userId); storedUserSettings.setId(userSettingsId); when(userSettingsRepository.findUserSettingsByUserId(userId)).thenReturn(storedUserSettings); UserSettings userSettings = this.userSettingsService.getUserSettingsByUser(user); Assert.assertEquals(userId, userSettings.getUserId()); Assert.assertEquals(userSettingsId, userSettings.getId()); verify(userSettingsRepository, times(0)).save(any()); }