### Question:
EnvironmentResourceService { public Credential getCredentialFromRequest(CredentialAwareEnvRequest request, String accountId) { Credential credential; if (StringUtils.isNotEmpty(request.getCredentialName())) { try { credential = credentialService.getByNameForAccountId(request.getCredentialName(), accountId, ENVIRONMENT); } catch (NotFoundException e) { throw new BadRequestException(String.format("No credential found with name [%s] in the workspace.", request.getCredentialName()), e); } } else { throw new BadRequestException("No credential has been specified in request for environment creation."); } return credential; } EnvironmentResourceService(CredentialService credentialService, NetworkService networkService, CloudPlatformConnectors cloudPlatformConnectors,
            CredentialToCloudCredentialConverter credentialToCloudCredentialConverter, Clock clock, ProxyConfigService proxyConfigService); Credential getCredentialFromRequest(CredentialAwareEnvRequest request, String accountId); Optional<ProxyConfig> getProxyConfig(String proxyConfigName, String accountId); boolean createAndUpdateSshKey(Environment environment); Optional<PublicKeyConnector> getPublicKeyConnector(String cloudPlatform); BaseNetwork createAndSetNetwork(Environment environment, NetworkDto networkDto, String accountId, Map<String, CloudSubnet> subnetMetas); void deletePublicKey(Environment environment); void deletePublicKey(Environment environment, String publicKeyId); boolean isPublicKeyIdExists(Environment environment, String publicKeyId);  }### Answer:
@Test void getCredentialFromRequest() { Credential value = new Credential(); when(credentialService.getByNameForAccountId(eq(ENVIRONMENT_NAME), eq(ACCOUNT_ID), any())).thenReturn(value); assertEquals(value, environmentResourceServiceUnderTest.getCredentialFromRequest(request, ACCOUNT_ID)); }
@Test void getCredentialFromRequestNotFound() { request.setCredentialName(ENVIRONMENT_NAME); when(credentialService.getByNameForAccountId(eq(ENVIRONMENT_NAME), eq(ACCOUNT_ID), any())).thenThrow(NotFoundException.class); assertThrows(BadRequestException.class, () -> environmentResourceServiceUnderTest.getCredentialFromRequest(request, ACCOUNT_ID)); }
@Test void getCredentialFromRequestInproperRequest() { request.setCredentialName(null); assertThrows(BadRequestException.class, () -> environmentResourceServiceUnderTest.getCredentialFromRequest(request, ACCOUNT_ID)); }