Posts Tagged ‘java’

Don’t look for things. Ask for them!

Before our previous article, our code to model a mechanic used to look like this:
class Mechanic {
private final Engine engine = ServiceLocator.getCar().getEngine();
public void fixEngine() { /* … */ }
}

Luckily, we applied some dependency injection, resulting in nicer code: testable, and with an explicit dependency on the ServiceLocator:
class [...]

Can’t test that Singleton? Try Dependency Injection!

So you want to test this method:

public class Client {

public int process(Params params) {
final Server server = Server.getInstance();
final Data data = server.retrieveDate(params);
// do stuff
[...]