Check the powermock documentation, depending on what you use, either mockito or easy mock. Below a sample based on mockito and a slightly modified version of your classes
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.*;
// use appropriate test runner
@RunWith(PowerMockRunner.class)
// prepare the class calling the constructor for black magic
@PrepareForTest(APIClient.class)
public class APIClientTest {
@Test
public void testCreateAPIClient() throws Exception {
// expectations
String expectedAccessKey = "accessKeyId";
String expectedSecretKey = "secretKey";
Credentials credentials = new Credentials(expectedAccessKey, expectedSecretKey);
// create a mock for your provider
CredentialsProvider mockProvider = mock(CredentialsProvider.class);
// make it return the expected credentials
when(mockProvider.getCredentials()).thenReturn(credentials);
// mock its constructor to return above mock when it's invoked
whenNew(CredentialsProvider.class).withAnyArguments().thenReturn(mockProvider);
// call class under test
Identity actualIdentity = APIClient.createIdentity();
// verify data & interactions
assertThat(actualIdentity.getAttribute(Identity.ACCESS_KEY), is(expectedAccessKey));
assertThat(actualIdentity.getAttribute(Identity.SECRET_KEY), is(expectedSecretKey));
}
}