How do you guys create utils classes? Is there a standard way?
Like the headline says you can either have something like:
public class Utils {
public static method1(){...}
public static method2(){...}
...
}
and use it by calling Utils.method1()
, but that way, when you write unit tests for the class that uses this utils class you cant mock the static methods so if they have a bug the unit test will fail.
Or, you can give an instance of the utils class in the constructor of the class using these utils and that way you can mock it in unit tests, but to me it looks very awkward giving an instance of a utils class to every class that uses it, and having to create an instance of a utils class in general.
I mean something like if we have class A that uses a method from the Utils class we can do: A a = new A(new Utils());
Is there a way that achieves both? Or at least something that is standard in the industry? Or another way that I didnt think of?