mercredi 29 mars 2017

Mocking not able to mock method call

I am trying to mock a call in my test but I am getting a error as its calling the real method than mocking it.

This is my method

public List<SecurityGroup> getAllSecurityGroups() {          
    AmazonEC2 ec2 = configSetter();     
    return awsService.getAllSecurityGroups(ec2);  
}

protected AmazonEC2 configSetter() {

    ProfileCredentialsProvider credentials = new ProfileCredentialsProvider(nonProdProfile);
    ClientConfiguration clientCfg = new ClientConfiguration();
    clientCfg.setProxyHost(this.proxyHost);
    clientCfg.setProxyPort(Integer.valueOf(proxyPort)); 
    clientCfg.setProtocol(Protocol.HTTPS);
    return new AmazonEC2Client(credentials, clientCfg);       

}

Here is my test class

@InjectMocks
    private AwsConfigurationLocal subject;

    @Mock
    private AwsService awsService;

    @Test
    public void TestgetAllSecurityGroups() throws Exception {

        ec2 = Mockito.mock(AmazonEC2Client.class);
        securityGroup = new SecurityGroup();
        List<SecurityGroup> result = Collections.singletonList(securityGroup);
        Mockito.when(awsService.getAllSecurityGroups(ec2)).thenReturn(result);  
        List<SecurityGroup> actual = subject.getAllSecurityGroups();
        assertThat(actual, CoreMatchers.is(equals(result)));   
    }

The test actually calls the protected method configSetter and fails when setting a proxy. Help me understand what I am doing wrong here.

Aucun commentaire:

Enregistrer un commentaire