I am writing a JUnit
test case for a method in which I am trying to catch an exception
but for some reason my test is not hitting the exception. Method is long but I just need to catch exception which is in the if
block.
Method under test:
public void createClient() throws EISClientException {
if( client == null ) {
ClientConfig c = new ClientConfig();
//Add Glocal JSON/Jackson Provider
c.register( JacksonFeature.class );
if ( isDisableSslValidation() ) {
try {
client = ClientBuilder.newBuilder().sslContext( SslUtil.createGullibleSslContext() )
.hostnameVerifier( SslUtil.gullibleVerifier ).withConfig( c ).build();
} catch( Exception e ) {
throw new EISClientException( e );
}
} else {
client = ClientBuilder.newBuilder().withConfig( c ).build();
}
//Add Client Properties
if( clientProperties.size() > 0 ) {
for( String key : clientProperties.keySet() ) {
client.property( key, clientProperties.get( key ) );
}
}
//Add Client Filters
if( clientFilters != null && clientFilters.size() > 0 ) {
for( IClientFilter cf : clientFilters ) {
client.register( cf.getFilter() );
}
}
}
}
JUnit test:
@Test
public void testCreateClientException() throws Exception {
ClientConfiguration clientConfiguration = new ClientConfiguration();
Client client = mock(Client.class);
boolean caughtException = false;
try {
clientConfiguration.setDisableSslValidation(true);
client = ClientBuilder.newBuilder().build();
clientConfiguration.createClient();
} catch (EISClientException ex) {
caughtException = true;
}
assertTrue(caughtException);
}
Looks like if the client is not built then throw exception
but if I pass null
as an argument in any of the methods surely I get NullPointerException
.
Any help would be appreciated.
Thanks