Scala best practices for mocking lazy vals

Viewed 177

I am new to Scala and am working with a large code base. There is a trait with a lazy val, and I want to unit-test one of the classes implementing that trait. For my unit tests I want to mock the lazy val.

My Java sensibilities tell me that I need to rewrite the trait so that it is more testable

Now to my actual question: Someone (with more Scala experience) suggested to me that I shouldn't refactor the production code. Instead it was suggested to me that in each unit test I should create an anonymous class of the class I want to test. In the anonymous class I can override the lazy val. Apparently this anonymous class testing is already being done elsewhere in the code base. But this seems like extremely bad practise to me. Am I wrong? Is this a standard practise for Scala developers?

I am very interested in hearing reasons 'why' this is a good or bad idea

0 Answers
Related