### Question:
InitService { public InitState getInitState() throws EntityNotFoundException { List<InitState> initStates = Lists.newArrayList(initStateRepository.findAll()); if (!initStates.isEmpty()) { InitState initState = initStates.get(0); initState.setRuntimeId(applicationContext.getId()); return initState; } else { throw new EntityNotFoundException(); } }  @EventListener(ApplicationReadyEvent.class) void onApplicationEvent(); InitState getInitState(); InitState saveInitState(InitState initState); boolean checkInitState(InitStateName initStateName); InitState setOAuthClientDetails(OAuthClientDetails oauthClientDetails); InitState setAdminUser(User adminUser, String password); InitState setAzureAdminUser(AdminUser adminUser);  }### Answer:
@Test public void getInitState_expectedInitState() { initState.setInitState(InitStateName.CREATE_USER); initState.setId(5L); iterable = Collections.singletonList(initState); when(initStateRepository.findAll()).thenReturn(iterable); InitState initStateFromService = initService.getInitState(); verify(initStateRepository).findAll(); assertEquals(initState.getInitState(), initStateFromService.getInitState()); assertEquals(initState.getId(), initStateFromService.getId()); }
@Test public void getInitState_expectException() { iterable = Collections.emptyList(); when(initStateRepository.findAll()).thenReturn(iterable); try { initService.getInitState(); fail(); } catch (Exception e) { assertTrue(e instanceof EntityNotFoundException); } }
@Test public void getInitState_returnsOnlyFirstEntry() { InitState initState = new InitState(); initState.setInitState(InitStateName.CREATE_USER); initState.setId(5L); InitState initState2 = new InitState(); initState2.setInitState(InitStateName.INITIALIZED); initState2.setId(1L); iterable = Arrays.asList(initState, initState2); when(initStateRepository.findAll()).thenReturn(iterable); InitState initStateFromService = initService.getInitState(); assertEquals(initState.getId(), initStateFromService.getId()); assertEquals(initState.getInitState(), initStateFromService.getInitState()); }
@Test public void getInitState_setsRuntimeId() { initState.setInitState(InitStateName.CREATE_USER); initState.setId(5L); iterable = Collections.singletonList(initState); when(initStateRepository.findAll()).thenReturn(iterable); when(applicationContext.getId()).thenReturn("runtimeId"); InitState initStateFromService = initService.getInitState(); verify(initStateRepository).findAll(); assertEquals(initState.getInitState(), initStateFromService.getInitState()); assertEquals("runtimeId", initStateFromService.getRuntimeId()); assertEquals(initState.getId(), initStateFromService.getId()); }