How would you compare Scala’s object testability compare to testing statics in Java? Any pitfalls and traps?
Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The same as statics in Java – references to objects are hard to swap for testing purposes, so you generally want to avoid them, except for side-effect-free, stateless utility methods, like Math.min, Math.max. Like in case of Java statics, objects holding state make testing particulary hard.
Scala objects have one advantage over the Java static, though – you can make your object extend some supertype and refer to through that interface. Then you can inject that object to the constructor of your client class, so your client gets more testable:
With plain Java static, that is impossible. You can do similar thing with singletons in Java (to which Scala objects are similar).