Users { @GET public List<User> get() { try { return useradmin.getUsers(); } catch (AuthserviceException e) { logservice.log(LogService.LOG_ERROR, "REST API endpoint /ukelonn/api/users failed with exception", e); throw new InternalServerErrorException("See log file for details"); } }  @GET List<User> get();  }
@Test public void testGet() { Users resource = new Users(); UserManagementService useradmin = mock(UserManagementService.class); when(useradmin.getUsers()).thenReturn(getUsersForUserManagement()); resource.useradmin = useradmin; List<User> users = resource.get(); assertThat(users).isNotEmpty(); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testGetWithSqlException() throws Exception { Users resource = new Users(); UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); PreparedStatement statement = mock(PreparedStatement.class); ResultSet results = mock(ResultSet.class); when(results.next()).thenThrow(SQLException.class); DataSource datasource = mock(DataSource.class); Connection connection = mock(Connection.class); when(datasource.getConnection()).thenReturn(connection); when(connection.prepareStatement(anyString())).thenReturn(statement); when(statement.executeQuery()).thenReturn(results); useradmin.setDataSource(datasource); resource.useradmin = useradmin; MockLogService logservice = new MockLogService(); resource.logservice = logservice; useradmin.setLogservice(logservice); resource.get(); fail("Should never get here"); }