I really got into TDD and I started using mockito in jUnit to improve my ability to test code. I really love mockito!
I noticed that I need to change the way I think about coding, for example, passing as many collaborators as possible to methods and limiting the work that is done in constructors where possible.
The following scenario justified some expert advice here on SO.
Let's say I have a method that will call some static methods for a specific class. For example.
public void method(){ OtherClass.staticMethod(); }
This is generally bad, but it is necessary in my scenario. To make the code more testable in my unit tests, I would like to avoid dependency on OtherClass and pass it as an argument.
This does not work, as it gives a compile-time error.
public void method(Class<? extends OtherClass> util){ util.staticMethod(); } ... method(OtherClass.class);
This will work, but I don't like creating an instance of OtherClass if I don't need it, since this is just a class of static utility, such as methods:
public void method(OtherClass util){ util.staticMethod(); } ... method(new OtherClass());
My question to you is: Is there a preferable way to accomplish this without using a new keyword?
jsdevel
source share