I have a package object as :
package object service {
var a = new A()
val b = new B()
}
and I have scala case classes where these variables from package are used as :
case class function () extends Logging with Serializable {
a.callFunctioninA();
}
How do I mock the package object a in scala test so I can stub the callFunctioninA to return/do nothing?
or is there a way we can mock new object call for **new A()**
in scala using mockito-scala
??
currently when I call the function it's trying to create real object A from service and wondering how we can mock object of class A so can be used to stub callFunctioninA
At the moment of writing this answer, there is only support for jdk 8, 11 and 14 and scala 2.11, 2.12 and 2.13.
Since mockito-scala 1.16.0 you can mock an object but not a package object
That being said and as @Mateusz Kubuszok detailed in comments
I would say that you have a design problem. You should not put things in objects if you need to mock them. Such as a database connection handler, a kafka client, a http client of a third party service, a service with business domain logic, etc.
They are useful to contain utility methods such as some kind of transformation from one type of class to another one, a helper method to validate something that doesn't depends on other components, a factory method, etc.
The example you provided contains a mutable value that is also a shared value at the same time
This could lead concurrent problems such as race conditions