Reputation: 43
I have the below method in the @Configuration
class and I need to write JUnit for this. Can anyone please guide me to achieve the JUnit test case?
@Bean
@Profile({"!test & !local"})
@Primary
public DiscoveryClient.DiscoveryClientOptionalArgs getClient() {
try {
DiscoveryClient.DiscoveryClientOptionalArgs args = new DiscoveryClient.DiscoveryClientOptionalArgs();
args.setAdditionalFilters(Collections.singletonList(new HTTPBasicAuthFilter(this.username, this.password)));
args.setSSLContext(sslContext());
System.setProperty("javax.net.ssl.trustStore", this.truststorePath);
System.setProperty("javax.net.ssl.trustStorePassword", this.trustStorePassword);
return args;
} catch (IOException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException e) {
throw new IllegalStateException("Unable to instantiate ssl context or authentication", e);
}
}
Upvotes: 1
Views: 5397
Reputation: 14806
First, consider what do you want to test. Successful creation of DiscoveryClient
? Sure. Taking a pragmatic stance, I would even avoid Spring in that case:
public class DiscoveryClientConfigTest {
@Test
public void testGetClient() {
DiscoveryClientConfig config = new DiscoveryClientConfig();
DiscoveryClientOptionalArgs client = config.getClient();
// Assert some state of the client
}
@Test
public void testGetClient_FailsToCreateClient() {
// Whatever exception you expect...
assertThrows(IOException.class, () -> {
DiscoveryClientConfig config = new DiscoveryClientConfig();
config.getClient();
});
}
}
If you want to test it while it affects some other components, then that's out of the scope of a unit test.
Upvotes: 1