0

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

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588

2 Answers2

0

You are setting up a mock client before you execute your test. In the method that you are testing, it starts if (client == null) I think this is evaluating to false and nothing is being processed in the method

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

Actually, in JUnit there is a more concise way of verifying an exception was thrown:

@Test(expected=EISClientException.class)
public void testCreateClientException() throws Exception {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    Client client = mock(Client.class);
    clientConfiguration.setDisableSslValidation(true);
    client = ClientBuilder.newBuilder().build();
    clientConfiguration.createClient();
}

This test will only succeed if the exception is thrown (I'm aware this doesn't directly answer your question)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588