I have a similar problem as mentioned in link: How to mock a function within Scala object using Mockito?
I have an object FooInner
with method stringData
, which takes int and give string output
object FooInner {
def stringData(data: Int): String = {
data match {
case 1 => "one"
case _ => "else"
}
}
}
Another object, FooOuter
, calls FooInner
to get string data and perform some operation on it.
object FooOuter {
def dummyCall(data: Int): String = {
FooInner.stringData(data) + "some operation"
}
}
My aim is to test method FooOuter.dummyCall
for string data, NOT returned by FooInner.stringData
For same, I followed above mentioned post and created trait
trait TFooInner {
def stringData(data: Int): String
}
Changed signature of FooInner to
object FooInner extends TFooInner {..}
And created test class FooTests
class FooTests extends FlatSpec with Matchers with MockitoSugar{
"dummyCall" should "mocked data" in {
val service = mock[TFooInner]
when(service.stringData(1)).thenReturn("1") // mocked service data -> 1
assert(FooOuter.dummyCall(1) === "1-some operation") // TestFailedException: "[one]-some operation" did not equal "[1]-some operation"
}
}
, but I am still not able to get mocked service data "1". I have following questions:
- How can I make FooOuter testable with data, not returned from FooInner
- Is it right functional style to code in Scala? What I feel is FooOuter is now tightly-coupled/dependent on FooInner
Scala: 2.11
Mockito: 1.9.5