### Question:
UserRoleFromContextService { public UserContextRole getUserRoleInUnit(OkrUnit okrUnit) { if (okrUnit instanceof OkrDepartment) { return getUserRoleInDepartment((OkrDepartment) okrUnit); } else { return getUserRoleWithoutContext(); } } @Autowired  UserRoleFromContextService(
      UnitRepository<OkrChildUnit> unitRepository,
      ObjectiveRepository objectiveRepository,
      KeyResultRepository keyResultRepository,
      NoteRepository noteRepository,
      AdminUserRepository adminUserRepository,
      UserService userService); UserContextRole getUserRoleWithoutContext(); UserContextRole getUserRoleInUnitId(Long unitId); UserContextRole getUserRoleInObjectiveId(Long objectiveId); UserContextRole getUserRoleInKeyResultId(Long keyResultId); UserContextRole getUserRoleInNoteId(Long noteId); UserContextRole getUserRoleInUnit(OkrUnit okrUnit);  }### Answer:
@Test public void getRoleFromUnit_Department_User_expectedUserRole() { when(currentUser.getId()).thenReturn(unrelatedUserId); OkrChildUnit okrChildUnit = new OkrDepartment(); UserContextRole actualRole = userRoleFromContextService.getUserRoleInUnit(okrChildUnit); Assert.assertEquals(UserContextRole.USER, actualRole); }
@Test public void getRoleFromUnit_Department_Master_expectedManagerRole() { when(currentUser.getId()).thenReturn(okrMasterId); OkrDepartment okrDepartment = new OkrDepartment(); okrDepartment.setOkrMasterId(okrMasterId); UserContextRole actualRole = userRoleFromContextService.getUserRoleInUnit(okrDepartment); Assert.assertEquals(UserContextRole.OKRMANAGER, actualRole); }
@Test public void getRoleFromUnit_Department_TopicSponsor_expectedManagerRole() { when(currentUser.getId()).thenReturn(okrThemenpateId); OkrDepartment okrDepartment = new OkrDepartment(); okrDepartment.setOkrTopicSponsorId(okrThemenpateId); UserContextRole actualRole = userRoleFromContextService.getUserRoleInUnit(okrDepartment); Assert.assertEquals(UserContextRole.OKRMANAGER, actualRole); }
@Test public void getRoleFromUnit_Department_Member_expectedMemberRole() { when(currentUser.getId()).thenReturn(okrMemberId); OkrDepartment okrDepartment = new OkrDepartment(); okrDepartment.setOkrMemberIds(Collections.singleton(okrMemberId)); UserContextRole actualRole = userRoleFromContextService.getUserRoleInUnit(okrDepartment); Assert.assertEquals(UserContextRole.OKRMEMBER, actualRole); }
@Test public void getRoleFromUnit_OkrBranch_User_expectedUserRole() { when(currentUser.getId()).thenReturn(unrelatedUserId); OkrChildUnit okrChildUnit = new OkrBranch(); UserContextRole actualRole = userRoleFromContextService.getUserRoleInUnit(okrChildUnit); Assert.assertEquals(UserContextRole.USER, actualRole); }
@Test public void getRoleFromUnit_OkrBranch_Admin_expectedAdminRole() { when(currentUser.getId()).thenReturn(adminId); OkrChildUnit okrChildUnit = new OkrBranch(); UserContextRole actualRole = userRoleFromContextService.getUserRoleInUnit(okrChildUnit); Assert.assertEquals(UserContextRole.ADMIN, actualRole); }