ScopeService { @Transactional public Scope save(final Long apiId, Scope scope) { final Api api = apiService.find(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); final Scope scopeData = scopeRepository.findByApiIdAndName(apiId, scope.getName()); HeimdallException.checkThrow(scopeData != null, SCOPE_INVALID_NAME); HeimdallException.checkThrow(scope.getOperations() == null || scope.getOperations().isEmpty(), SCOPE_NO_OPERATION_FOUND); scope.getOperations().forEach(op -> { Operation operation = operationRepository.findOne(op.getId()); HeimdallException.checkThrow( operation == null, SCOPE_INVALID_OPERATION, op.getId().toString()); HeimdallException.checkThrow( !operation.getResource().getApi().getId().equals(apiId), SCOPE_OPERATION_NOT_IN_API, operation.getId().toString(), apiId.toString()); }); scope.setApi(api); scope = scopeRepository.save(scope); amqpCacheService.dispatchClean(); return scope; }  @Transactional(readOnly = true) Scope find(final Long apiId, final Long scopeId); @Transactional(readOnly = true) ScopePage list(final Long apiId, final ScopeDTO scopeDTO, final PageableDTO pageableDTO); @Transactional(readOnly = true) List<Scope> list(final Long apiId, final ScopeDTO scopeDTO); @Transactional Scope save(final Long apiId, Scope scope); @Transactional void delete(final Long apiId, final Long scopeId); @Transactional Scope update(final Long apiId, final Long scopeId, Scope scope);  }
@Test public void saveTest() { Mockito.when(apiService.find(Mockito.anyLong())).thenReturn(scope.getApi()); Mockito.when(scopeRepository.findByApiIdAndName(Mockito.anyLong(), Mockito.anyString())).thenReturn(null); Mockito.when(scopeRepository.save(Mockito.any(Scope.class))).thenReturn(scope); Mockito.when(operationRepository.findOne(1L)).thenReturn(operation); Mockito.when(operationRepository.findOne(2L)).thenReturn(operation2); Mockito.when(operationRepository.findOne(3L)).thenReturn(operation3); Scope scopeSaved = scopeService.save(scope.getApi().getId(), scope); assertEquals(scope, scopeSaved); }