I've a singleton class in Scala that's declared as an 'object'. I need to write unit tests that 'mock' methods in this object. How do I do it? I tried using 'PowerMockRunner' etc with no luck.
For example:
object MyClass extends Serializable {
def apply(): Unit = { ..... }
def more methods....
}
Now I need to write a unit test that mocks the 'apply' method. How do I do it?
The short answer - You shouldn't unit-test singletons.
The long is here - Unit testing with singletons.
You can extract the functionality to a trait and unit-test the trait.
Not sure why you need the extends Serializable
but left it just to show what I mean
object MyClass extends Serializable with TheFunctionality {
// whatever you need here
}
trait TheFunctionality {
def apply(): Unit = ???
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With