Yes, it's a smell: Hard-to-Test Code
I'd start with the following:
- Create an interface for
MyApiClient(usually it's easier to mock interfaces than classes), - Remove the static modifier of
signin, - Create a constructor in
Signwhich get aMyApiClientinstance and store it in a field, - Modify the
signinmethod to use the instance above.
public class Sign {
private final MyApiClient client;
public Sign(final MyApiClient client) {
this.client = client;
}
public void signin(final String username, final String password,
final SigninResponseHandler responseHandler) {
...
client.post(path, params, ...
Here you can create a Sign object with a mocked MyApiClient which is easier to test.
Some other notes:
Signdoes not seem a good class name. From Clean Code, page 25:Classes and objects should have noun or noun phrase names like
Customer,WikiPage,Account, andAddressParser. [...] A class name should not be a verb.MyApiClientalso should be renamed something meaningful.
Some useful readings:
- Eliminating static helper classesEliminating static helper classes
- TotT: Using Dependency Injection to Avoid SingletonsTotT: Using Dependency Injection to Avoid Singletons
- Effective Java, Second Edition, Item 16: Favor composition over inheritance.