0

My code structure is like below:

class A {
  def a(x: () => Unit) { do something}
}

class B {
  .... 
  def foo() {
    def x() { something }
    a(x)
  }
}

Now I want to do unittest of class B with a mock A.

val a = mock[A]
def x () { ... }
a.a(x) atLeastOnce

The above doesn't work. Since this new x is not the x inside foo(). But the x inside foo is a local one, not accessible to unittest. Any suggestion except to move x out of foo?

Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
user398384
  • 1,124
  • 3
  • 14
  • 21
  • I do want to test B. A is an external class relying on DB -- means I have to mock A in the unittest. Since A.a needs a callback function as its parameter, I wrote x(). – user398384 Apr 19 '11 at 02:54
  • It sounds like you want to express that `A.a` is called at least once, but you don't care about the parameter that is used in the call. I don't know EasyMock, but mocking frameworks usually express such expectations with something like `a.a(any) atLeastOnce`. If you *do* care about the closure (e.g. you want to verify that it has a particular side-effect), you could capture the actual argument and invoke it (to fake `A`'s role). – Aaron Novstrup Apr 19 '11 at 03:03

1 Answers1

0

You have to mock out the function literal passed into A.a. Please look into the answer of the following SOF question and see whether that helps

How to mock a method with functional arguments in Scala?

Community
  • 1
  • 1
Nilanjan
  • 741
  • 4
  • 9