This class depends on the Networker
instance. Your goal is to substitute this dependency in tests. There are several ways to do that:
- Use service locator
- User IoC
- Mock
Networker.getInstance()
Service Locator
In this case Slave
shouldn't know where to get instance of Networker
. It should get instance from central registry of services. This means that you need to have ServiceLocator
class which basically would store link to Networker
instance and you can replace it in tests.
IoC pattern
This is somewhat similar to Service Locator because Slave
doesn't know where to get instance of Networker
. This dependency should be provided externally (aka injected) in this case via constructor like this:
public Slave(Player player, Networker networker) {
// same as above but networker passed to method is used
}
In this case you can use another instance of networker (or mock or stub) specifically designed for your test.
Directly mock static call
Some mock frameworks allow mocking static methods. You can mock call to Networking.getInstance()
so that you can check that call happened with correct parameters.
This is however not recommended and I discourage you to use this approach. See also this and this