How to mock a nested methods of a Scala Object using Mockito? - scala

I am trying to create Unit tests for a Scala application. I am trying mock a method which is inside a scala object with Mockito.
The method am trying to mock is calling a method of another object from different package.
package1 -> method 1 (This is the method , which I am trying to mock its return output using MOckito )
package2 -> method 2, method 3
'''def method 1
{
//some code
calling method 2()
}
def method 2
{
///some code
calling method 3
}
I tried to mock the method 1 . But getting error 'NullPointerException' for method 2 and 3 . I have read about RETURNS_DEEP_STUBS . But all my methods are define under Scala objects not under Class. Can we mock Scala Objects using REURNS_DEEP_STUBS?
Could anyone suggest some ideas?

Related

Callable object in Kotlin or Swift

Is it possible to create a callable object in Kotlin and/or Swift?
I have many objects that have just one method execute. The object is basically used as a closure. Some dependencies are captured during object construction. Some arguments are passed when the execute method is called.
The reason I’m not using plain functions is the classes implement interfaces and type checking is much more helpful though out the program.
You can overload the invoke operator:
class Callable(val prop: String) {
operator fun invoke(para : String) {
println("Invoke: $prop $para")
}
}
fun main() {
val c = Callable("prop")
c("para")
}

Scala mock not mocking extended class function?

Here is how the code looks:
class B {
doStuff() //some API call
}
class A extends B {
val x = doStuff()
...
}
When I mock it as follows:
class ASpec {
val a = new A
when(a.doStuff()).thenReturn("stuff") <---this should just return "stuff" on the test
assert(true, a.doOtherStuff())
}
Problem is that its definitely still making the API call from doStuff(). How come its not returning "stuff" as specified?
You need to make A a mock. Right now, you're creating a real instance of A via new A. Instead, use mock(classOf[A]) (or, with ScalaTest's MockitoSugar, mock[A]):
val a = mock(classOf[A])
when(a.doStuff()).thenReturn("stuff")
when(a.doOtherStuff()).thenCallRealMethod() // Necessary since `A` is a mock
assert(a.doOtherStuff())
However, it's generally a Bad Idea™ to mock one method of a class so that you can test another. For one thing, you'll need to remember to specify the behavior of each method of A used by doOtherStuff (potentially using thenCallRealMethod on all of them). Prefer only mocking things external to your class. To accomplish this, you can either mock what doStuff depends on or move doStuff to another class.

ScalaMock Inheritied Trait Function of Object ScalaTest

I am attempting to test a function, however the function that I am testing makes a call to the traits function which I would like to stub. I can't seem to stub this function using ScalaMock, as I am unable to mock the object.
trait[A<:CommonReturn] commonTrait[A] {
def commonFunction(s:String):(String,String) = {
("Hello","World")
}
def testMe(s:String) : A
}
This trait is then extended by many Objects each implementing commonTrait and returning their specific sub-type of common return.
object ob extends commonTrait[ConcreteType] {
override def testMe(s:String){
val(x,y) = commonFunction(s)
val z = "unique logic"
ConcreteType(x,y,z)
}
}
I therefore am now trying to test ob.testMe however I can't seem to Mock the ob Object, therefore can't stub the commonFunction.
Is this due to my architecture? Or is it possible to mock an object with scalamock and use scalatest?
val mocked = mock[ob]
(mocked.commonFunction _).expect(*).returning("test","test")
This doesn't compile.
you cannot mock objects with ScalaMock, as a mock[X] is a subclass of X. Scala does not allow subclasses of objects.
If you need to test collaboration with this commonFunction then inheritance makes it rather difficult. I would consider designing this with Dependency Injection instead.

Understanding method invocation in Scala

I'm pretty new to Scala and came from Java and got confused by some piece of code when reading this documentation. Here is the code.
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
Where path("hello") is the method of trait:
trait PathDirectives /*extends omitted*/ {
def path[L](pm: PathMatcher[L]): Directive[L] = pathPrefix(pm ~ PathEnd)
// the rest omitted
}
So, when we invoke the path("hello") method we would need an object implementing the trait to invoke it on. But in the example it was just a method invocation. Just like a static method.
What did I miss?
So, when we invoke the path("hello") method we would need an object implementing the trait to invoke it on.
Yes, and that object is akka.http.scaladsl.server.Directives. The reason that you don't need to write Directives.path is that the code imports Directives._, so you can call Directives' methods directly (similar to a static import in Java except the method doesn't have to be static).

How do I mock static function (Object function, not class function) in scala

Object A {
def a = { something}
}
// I've import A, but still have error message: not found: type A
val x = mock[A]
You don't. Not only A is not a type or class -- it is an instance -- but it is an instance of a singleton (A.type).
What you do instead is put your methods on a trait, and make the object extend it. Then, you mock the trait instead of mocking the object.
You may find this email thread instructive.
Whilst pure mocking of the object is not possible with any tool yet, the thread above does have a few options for you. All of which involve changing your design to some degree.